大家好,今天测试EJB3.0的JMS相关内容,测试了一下远程消费,步骤和出现的异常如下:
1、写一个生产者:
package com.foshanshop.ejb3.app;
import java.util.Properties;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.foshanshop.ejb3.bean.Man;
public class QueueSender {
public static void main(String[] args) {
QueueConnection conn = null;
QueueSession session = null;
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.setProperty(Context.PROVIDER_URL, "10.1.35.126:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
conn = factory.createQueueConnection();
session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Destination destination = (Queue) ctx.lookup("queue/foshanshop");
MessageProducer producer = session.createProducer(destination);
//发送文本
TextMessage msg = session.createTextMessage("佛山人您好,这是我的第一个消息驱动Bean");
producer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
session.close ();
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
2、在JMS服务器上布置服务
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=foshanshop">
<attribute name="JNDIName">queue/foshanshop </attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=chatTopic">
<attribute name="JNDIName">topic/chatTopic </attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
</mbean>
</server>
3、在另外一台机器上布置消费者(该机器也安装了JBOSS,打算将消费者布置在JBOSS里面)
package cn.tcl.remotefoshandrivebean;
import java.io.ByteArrayOutputStream;