我今天测试了一下,按照我上面说的步骤,建好weblogic jms和mdb 然后在onmessage里加一行代码。 System.out.println("have received message"); 然后打包发布。 然后建立客户端,代码是这样的: package mdbdemo;import java.rmi.RemoteException;import java.util.Properties;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import javax.jms.TextMessage;import javax.jms.Topic;import javax.jms.TopicConnection;import javax.jms.TopicConnectionFactory;import javax.jms.TopicPublisher;import javax.jms.TopicSession;import javax.ejb.CreateException;import javax.ejb.RemoveException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;/** * This class illustrates calling a Message-Driven bean and publishing * quotes on a topic. * * @author Copyright (c) 1998-2002 by BEA Systems, Inc. All Rights Reserved. */public class TestClient { static private String TOPIC_NAME = "MDBDemo Topic"; private String m_url; private Context m_context; private TopicConnection m_topicConnection; public TestClient(String url) throws NamingException { m_url = url; try { // // Create a context // m_context = getInitialContext(); // // Create the connection and start it // TopicConnectionFactory cf = (TopicConnectionFactory) m_context.lookup("MDBDemoCF"); m_topicConnection = cf.createTopicConnection(); m_topicConnection.start(); } catch(Exception ex) { ex.printStackTrace(); } } /** * Runs this example from the command line. Example: * <p> * <tt>java examples.ejb20.message.Client "t3://localhost:7001"</tt> * <p> * The parameters are optional, but if any are supplied, * they are interpreted in this order: * <p> * @param url URL such as "t3://localhost:7001" of Server */ public static void main(String[] args) throws Exception { log("\nBeginning message.Client...\n"); String url = "t3://localhost:7001"; TestClient client = null; try { client = new TestClient(url); } catch (NamingException ne) { System.exit(1); } try { client.example(); } catch (Exception e) { log("There was an exception while creating and using the MDB."); log("This indicates that there was a problem communicating with the server: "+e); //e.printStackTrace(); } log("\nEnd message.Client...\n"); } /** * Runs this example. */ public void example() throws RemoteException, JMSException, NamingException { Topic newTopic = null; TopicSession session = null; try { session = m_topicConnection.createTopicSession(false, // non transacted Session.AUTO_ACKNOWLEDGE); newTopic = (Topic) m_context.lookup(TOPIC_NAME); } catch(NamingException ex) { newTopic = session.createTopic(TOPIC_NAME); m_context.bind(TOPIC_NAME, newTopic); } TopicPublisher sender = session.createPublisher(newTopic); TextMessage tm = session.createTextMessage(); String[] quotes = new String[] { "BEAS 40 1/8", "SUNW 79 1/2", "IBM 82 1/4", "Hello !" }; for (int i = 0; i < quotes.length; i++) { tm.setText(quotes[i]); sender.publish(tm); } } /** * Using a Properties object will work on JDK 1.1.x and Java2 * clients */ private Context getInitialContext() throws NamingException { try { // Get an InitialContext Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, m_url); return new InitialContext(h); } catch (NamingException ex) { log("We were unable to get a connection to the WebLogic server at "+m_url); log("Please make sure that the server is running."); throw ex; } } /** * This is the Java2 version to get an InitialContext. * This version relies on the existence of a jndi.properties file in * the application's classpath. * */// private static Context getInitialContext()// throws NamingException// {// return new InitialContext();// } private static void log(String s) { System.out.println(s); }}/// 运行以后,在weblogic的dos窗口,就可以看到:have received message have received message have received message have received message
......(略)
这是一个实验,代码很简单,有了它,后面的功能你就自己填吧!
顺便曾今研究过的兄弟姐妹也给点意见,看看为什么接受不到消息。有建设性的意见就发分JavaBean:(生成消息)
package Bean;import java.io.Serializable;
import java.rmi.RemoteException;import javax.ejb.*;
import javax.jms.*;
import jmsejb.*;
import javax.naming.*;
import java.util.*;import weblogic.jms.ServerSessionPoolFactory;
import weblogic.jms.extensions.*;public class sessionBeanTest {
public sessionBeanTest(){
try{
sessionBeanTest();
}catch(Exception e){
System.out.println("消息产生出错!!");
}
}
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
public final static String Queue="weblogic.examples.jms.exampleQueue";
MessageListener listener;
public void sessionBeanTest()throws Exception{
/* 处理订单的逻辑从此处开始*/
Context jndiEnc = null;
QueueConnectionFactory Queuefactory=null;
QueueConnection con=null;
QueueSession session=null;
QueueSender queuesender=null;
QueueReceiver qreceiver=null;
Queue queue=null;
TextMessage message ;
final int NUM_MSGS;
try{
System.out.println("取得JNDI的情景引用");
jndiEnc=getInitialContext("t3://localhost:7001", "lyu", "netscape");
// 使用 JNDI ENC 获取 JMS 工厂和主题标识符
Queuefactory = (QueueConnectionFactory)jndiEnc.lookup(JMS_FACTORY);
queue = (Queue)jndiEnc.lookup(Queue);
}catch(NamingException e){
System.out.println("JNDI lookup failed: " + e.toString());
}
try{
System.out.println("发布一个TestMessag");
// 产生一个用来发送消息的发布者
con = Queuefactory.createQueueConnection();
session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE );
//将一个 TextMessage 发送给主题(虚拟通道)
queuesender = session.createSender(queue);
/*处理订单以后,向其它系统发送有关此订单的一条消息*/
message = session.createTextMessage();
message.setJMSCorrelationID("19791121");
System.out.println("message.setJMSCorrelationID(19791121)");
message.setJMSType("liuyu");
System.out.println("message.setJMSType(liuyu)");
queuesender.send(message);
con.start(); if(con!=null)
con.close();
}catch(Exception e){
System.out.println("error!!!");
} }
static Context getInitialContext(String url, String user, String password)
throws Exception
{
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
if (user != null) {
h.put(Context.SECURITY_PRINCIPAL, user);
h.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
} return new InitialContext(h);
}}ejb:(接受消息)
import java.util.*;
import java.text.*;
import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;public class MessageBean implements MessageListener, MessageDrivenBean {
private transient MessageDrivenContext messageDrivenContext;
QueueReceiver qreceiver=null;
public MessageBean(){
}
public void ejbCreate() throws CreateException {
}
public void ejbRemove() {
}
public void onMessage(Message msg) {
System.out.println("<<<<MessageBean>>>>");
try{
if(msg instanceof TextMessage ){
TextMessage text = (TextMessage)msg;
String Id = "JMSCorrelationID = "+text.getJMSCorrelationID();
String type = "JMSType = "+text.getJMSType();
System.out.println(Id);
System.out.println(type);
}else{
System.out.println("type is err!!");
}
}catch(Exception e){
System.out.println("erro!!");
}
}
public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) {
this.messageDrivenContext = messageDrivenContext;
}
}
非常感谢,只是现在我还是接受不到消息,你看看我的代码,最终的结果是不是应该为JAVABean和EJB的结果同时在后台答应出来?可是我的没有,你看是不是EJB写的有问题?
mdb相当与接受消息的客户端吧。
<message-drive>有个子标签:
<message-selector>,可以定义选择那种类型的消息。比如:
<message-selector>
JMSType='log'AMD logLeve='server'
</message-selector>
我猜想是这样的。
queue or topic会给每个MDB发送消息,MDB会用instance of来判断消息是否是想要的。
如果是的话,就处理。
当然,可以想上面选择消息。
我学j2ee才一个月,最近忙用jbuider+weblogic实际开发,所以我也不熟。
我有代码,
不过太长。
我也可以告诉你他们是怎么合作的。
//我现在在做毕业设计,用j2ee+rep的知识,累死了。遇到很多问题,只能来这里救助了。
我不知道怎么修改文章。所以没有修改
然后在onmessage里加一行代码。
System.out.println("have received message");
然后打包发布。
然后建立客户端,代码是这样的:
package mdbdemo;import java.rmi.RemoteException;import java.util.Properties;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import javax.jms.TextMessage;import javax.jms.Topic;import javax.jms.TopicConnection;import javax.jms.TopicConnectionFactory;import javax.jms.TopicPublisher;import javax.jms.TopicSession;import javax.ejb.CreateException;import javax.ejb.RemoveException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;/** * This class illustrates calling a Message-Driven bean and publishing * quotes on a topic. * * @author Copyright (c) 1998-2002 by BEA Systems, Inc. All Rights Reserved. */public class TestClient { static private String TOPIC_NAME = "MDBDemo Topic"; private String m_url; private Context m_context; private TopicConnection m_topicConnection; public TestClient(String url) throws NamingException { m_url = url; try { // // Create a context // m_context = getInitialContext(); // // Create the connection and start it // TopicConnectionFactory cf = (TopicConnectionFactory) m_context.lookup("MDBDemoCF"); m_topicConnection = cf.createTopicConnection(); m_topicConnection.start(); } catch(Exception ex) { ex.printStackTrace(); } } /** * Runs this example from the command line. Example: * <p> * <tt>java examples.ejb20.message.Client "t3://localhost:7001"</tt> * <p> * The parameters are optional, but if any are supplied, * they are interpreted in this order: * <p> * @param url URL such as "t3://localhost:7001" of Server */ public static void main(String[] args) throws Exception { log("\nBeginning message.Client...\n"); String url = "t3://localhost:7001"; TestClient client = null; try { client = new TestClient(url); } catch (NamingException ne) { System.exit(1); } try { client.example(); } catch (Exception e) { log("There was an exception while creating and using the MDB."); log("This indicates that there was a problem communicating with the server: "+e); //e.printStackTrace(); } log("\nEnd message.Client...\n"); } /** * Runs this example. */ public void example() throws RemoteException, JMSException, NamingException { Topic newTopic = null; TopicSession session = null; try { session = m_topicConnection.createTopicSession(false, // non transacted Session.AUTO_ACKNOWLEDGE); newTopic = (Topic) m_context.lookup(TOPIC_NAME); } catch(NamingException ex) { newTopic = session.createTopic(TOPIC_NAME); m_context.bind(TOPIC_NAME, newTopic); } TopicPublisher sender = session.createPublisher(newTopic); TextMessage tm = session.createTextMessage(); String[] quotes = new String[] { "BEAS 40 1/8", "SUNW 79 1/2", "IBM 82 1/4", "Hello !" }; for (int i = 0; i < quotes.length; i++) { tm.setText(quotes[i]); sender.publish(tm); } } /** * Using a Properties object will work on JDK 1.1.x and Java2 * clients */ private Context getInitialContext() throws NamingException { try { // Get an InitialContext Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, m_url); return new InitialContext(h); } catch (NamingException ex) { log("We were unable to get a connection to the WebLogic server at "+m_url); log("Please make sure that the server is running."); throw ex; } } /** * This is the Java2 version to get an InitialContext. * This version relies on the existence of a jndi.properties file in * the application's classpath. * */// private static Context getInitialContext()// throws NamingException// {// return new InitialContext();// } private static void log(String s) { System.out.println(s); }}///
运行以后,在weblogic的dos窗口,就可以看到:have received message
have received message
have received message
have received message
Connection factory name填进去吗?Resource References标签的JNDI name 是不是JMS的工厂JNDI name?我还是接受不到消息,苦闷啊!
请问是怎么解决的??我的程序运行时,可以接受,当队列空的时候,再加入消息,这回那个Listener就不管用了。