初学Message-Driven Bean,想在JBoss上试一下,却发现这真是一个痛苦的过程。
Message-Driven Bean: import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.MessageDrivenBean; import javax.ejb.MessageDrivenContext; import javax.ejb.CreateException; import javax.naming.*; import javax.jms.*;
public class LogBean implements MessageDrivenBean, MessageListener {
private transient MessageDrivenContext mdc = null; private Context context; public LogBean() { System.out.println("In SimpleMessageBean.SimpleMessageBean()"); }
public void setMessageDrivenContext(MessageDrivenContext mdc) { System.out.println("In " + "SimpleMessageBean.setMessageDrivenContext()"); this.mdc = mdc; }
public void ejbCreate() { System.out.println("In SimpleMessageBean.ejbCreate()"); }
public void onMessage(Message inMessage) { TextMessage msg = null;
try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println("MESSAGE BEAN: Message received: " + msg.getText()); } else { System.out.println("Message of wrong type: " + inMessage.getClass().getName()); } } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); } } // onMessage public void ejbRemove() { System.out.println("In SimpleMessageBean.remove()"); }
} // class
客户端: TestClient.java import javax.naming.*;
import javax.jms.*; import java.util.*;
public class TestClient {
public static void main (String[] args) throws Exception {
// Initialize JNDI Context ctx = new InitialContext();
// 1: Lookup ConnectionFactory via JNDI TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
// 2: Use ConnectionFactory to create JMS connection TopicConnection connection = factory.createTopicConnection();
// 3: Use Connection to create session TopicSession session = connection.createTopicSession( false, Session.AUTO_ACKNOWLEDGE);
// 4: Lookup Desintation (topic) via JNDI Topic topic = (Topic) ctx.lookup("topic/testTopic");
// 5: Create a Message Producer TopicPublisher publisher = session.createPublisher(topic);
// 6: Create a text message, and publish it TextMessage msg = session.createTextMessage(); msg.setText("This is a test message."); publisher.publish(msg); } }
发布过程: ejb-jar.xml <?xml version="1.0"?> <!DOCTYPE ejb-jar> <ejb-jar> <enterprise-beans> <message-driven> <ejb-name>Log</ejb-name> <ejb-class>LogBean</ejb-class> <message-selector></message-selector> <transaction-type>Bean</transaction-type> <acknowledge-mode>Auto-acknowledge</acknowledge-mode> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> </message-driven-destination> </message-driven> </enterprise-beans> </ejb-jar>
jboss.xml <?xml version="1.0"?> <jboss> <enterprise-bean> <message-driven> <ejb-name>Log</ejb-name> <configuration-name>Standard Message Driven Bean</configuration-name> <destination-jndi-name>topic/testTopic</destination-jndi-name> </message-driven> </enterprise-bean> </jboss>
Log日志: ..................... 2003-04-02 19:48:23,921 INFO [org.jboss.deployment.MainDeployer] Starting deployment of package: file:/F:/jboss/server/default/deploy/ejb-test.jar 2003-04-02 19:48:23,968 INFO [org.jboss.ejb.EjbModule] Creating 2003-04-02 19:48:23,984 INFO [org.jboss.ejb.EjbModule] Deploying Log 2003-04-02 19:48:24,062 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Creating 2003-04-02 19:48:24,078 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Created 2003-04-02 19:48:24,078 INFO [org.jboss.ejb.EjbModule] Created 2003-04-02 19:48:24,078 INFO [org.jboss.ejb.EjbModule] Starting 2003-04-02 19:48:24,093 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Starting 2003-04-02 19:48:24,093 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Creating 2003-04-02 19:48:24,171 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Created 2003-04-02 19:48:24,187 WARN [org.jboss.ejb.plugins.jms.JMSContainerInvoker] destination not found: topic/Log reason: javax.naming.NameNotFoundException: Log not bound<--------错误 2003-04-02 19:48:24,187 WARN [org.jboss.ejb.plugins.jms.JMSContainerInvoker] creating a new temporary destination: topic/Log 2003-04-02 19:48:24,187 INFO [org.jboss.mq.server.jmx.Topic.Log] Creating 2003-04-02 19:48:24,187 INFO [org.jboss.mq.server.jmx.Topic.Log] Created 2003-04-02 19:48:24,187 INFO [org.jboss.mq.server.jmx.Topic.Log] Starting 2003-04-02 19:48:24,187 INFO [org.jboss.mq.server.jmx.Topic.Log] Bound to JNDI name: topic/Log 2003-04-02 19:48:24,203 INFO [org.jboss.mq.server.jmx.Topic.Log] Started 2003-04-02 19:48:24,281 WARN [org.jboss.mq.security.SecurityManager] No SecurityMetadadata was available for Log adding default security conf 2003-04-02 19:48:24,281 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Starting 2003-04-02 19:48:24,296 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Started 2003-04-02 19:48:24,296 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Started 2003-04-02 19:48:24,296 INFO [org.jboss.ejb.EjbModule] Started 2003-04-02 19:48:24,296 INFO [org.jboss.deployment.MainDeployer] Deployed package: file:/F:/jboss/server/default/deploy/ejb-test.jar 运行结果: 19:52:35,265 WARN [OILServerILService] Connection failure (1). java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at java.io.BufferedInputStream.fill(BufferedInputStream.java:183) at java.io.BufferedInputStream.read(BufferedInputStream.java:201) at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java :2133) at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Object InputStream.java:2316) at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStre am.java:2383) at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream .java:2455) at java.io.ObjectInputStream$BlockDataInputStream.readByte(ObjectInputSt ream.java:2604) at java.io.ObjectInputStream.readByte(ObjectInputStream.java:845) at org.jboss.mq.il.oil.OILServerILService$Client.run(OILServerILService. java:205) at java.lang.Thread.run(Thread.java:536) *******************************************************************************************************
jboss.xml,ejb-jar.xml是直接拷贝过来的。对于上面的错误始终不试不得其解,如是就
原封不动的拷贝一个实例,加上UTF-8后竟然上面的错误没有了。心里一阵狂喜,
可是。。。。。。 ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar> <enterprise-beans> <message-driven> <ejb-name>Log</ejb-name> <ejb-class>LogBean</ejb-class> <message-selector></message-selector> <transaction-type>Bean</transaction-type> <acknowledge-mode>Auto-acknowledge</acknowledge-mode> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> </message-driven-destination> </message-driven> </enterprise-beans> </ejb-jar> jboss.xml <?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-beans> <session> <ejb-name>Log</ejb-name> <configuration-name>Standard Message Driven Bean</configuration-name> <destination-jndi-name>topic/testTopic</destination-jndi-name> </session> </enterprise-beans> </jboss>
Log日志: .......................... 2003-04-02 20:15:57,671 INFO [org.jboss.deployment.MainDeployer] Starting deployment of package: file:/F:/jboss/server/default/deploy/ejb-test.jar 2003-04-02 20:15:57,796 INFO [org.jboss.ejb.EjbModule] Creating 2003-04-02 20:15:57,812 INFO [org.jboss.ejb.EjbModule] Deploying Log 2003-04-02 20:15:57,890 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Creating 2003-04-02 20:15:57,890 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Created 2003-04-02 20:15:57,890 INFO [org.jboss.ejb.EjbModule] Created 2003-04-02 20:15:57,890 INFO [org.jboss.ejb.EjbModule] Starting 2003-04-02 20:15:57,906 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Starting 2003-04-02 20:15:57,906 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Creating 2003-04-02 20:15:57,984 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Created 2003-04-02 20:15:58,093 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Starting<--错误没有了。 2003-04-02 20:15:58,093 INFO [org.jboss.ejb.plugins.jms.DLQHandler] Started 2003-04-02 20:15:58,109 INFO [org.jboss.ejb.plugins.jms.JMSContainerInvoker] Started 2003-04-02 20:15:58,109 INFO [org.jboss.ejb.EjbModule] Started 2003-04-02 20:15:58,109 INFO [org.jboss.deployment.MainDeployer] Deployed package: file:/F:/jboss/server/default/deploy/ejb-test.jar 运行结果: 20:20:15,921 INFO [STDOUT] In SimpleMessageBean.SimpleMessageBean() 20:20:15,937 INFO [STDOUT] In SimpleMessageBean.setMessageDrivenContext() 20:20:15,937 INFO [STDOUT] In SimpleMessageBean.ejbCreate() 20:20:15,937 INFO [STDOUT] MESSAGE BEAN: Message received: This is a test messa ge. 运行结果出来了,但是还是有错误。 20:20:16,031 WARN [OILServerILService] Connection failure (1). java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at java.io.BufferedInputStream.fill(BufferedInputStream.java:183) at java.io.BufferedInputStream.read(BufferedInputStream.java:201) at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java :2133) at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Object InputStream.java:2316) at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStre am.java:2383) at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream .java:2455) at java.io.ObjectInputStream$BlockDataInputStream.readByte(ObjectInputSt ream.java:2604) at java.io.ObjectInputStream.readByte(ObjectInputStream.java:845) at org.jboss.mq.il.oil.OILServerILService$Client.run(OILServerILService. java:205) at java.lang.Thread.run(Thread.java:536)
虽然结果出来了,但是还是有下面的错误。唉,没办法了,放一下吧,等熟悉了后在
回来看是什么原因吧。

|