通讯流程图,其中EJB扮演了client2的角色
首先是Server端的开发及设置:
1、增加一个用户:
bin\add-user.bat
用户名密码随便,但要属于guest组
2、启动Server
standalone.bat -server-config=standalone-full.xml
3、新建Topic
jboss-cli.bat --connect jms-topic add --topic-address=neoTopic --entries=topic/neoTopic,java:jboss/exported/jms/topic/neoTopic
4、新建java项目
4.1、写一个TopicBean
MessageTopicBean .java
package com.neohope.ejb.test; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.TextMessage; /** * Created by Hansen */ @MessageDriven(mappedName = "MessageTopicBean",activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/neoTopic"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") }) public class MessageTopicBean implements javax.jms.MessageListener { public MessageTopicBean() { } @Override public void onMessage(Message message) { try { System.out.println("MessageTopicBean.onMessage invoked"); if(message instanceof TextMessage) { TextMessage textMessage = (TextMessage)message; System.out.println("Message: " + textMessage.getText()); } else { System.out.println("Message is not a TextMessage"); } } catch (JMSException e) { e.printStackTrace(); } } }
4.2、打jar包,或者自己压缩为TestEJBServer.jar
5、新建一个ear项目
5.1增加application.xml文件
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6"> <description>EAR Test</description> <display-name>EAR Test</display-name> <module> <ejb>TestEJBServer.jar</ejb> </module> <library-directory>lib</library-directory> </application>
5.2打ear包,或自己压缩为TestEar.ear
5.3TestEar的结构为:
│ TestEJBServer.jar │ ├─lib └─META-INF application.xml
6.通过EAP进行部署
到这里服务端已经完成了。
然后是客户端的设置:
1、通过Topic方式访问
package com.neohope.ejb.test; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; /** * Created by Hansen */ public class TestTopicBean { public static void main(String[] args) throws NamingException, JMSException { final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); jndiProperties.put(Context.PROVIDER_URL, "remote://localhost:4447"); final InitialContext ctx = new InitialContext(jndiProperties); TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); TopicConnection connection = factory.createTopicConnection("user001", "user001#"); TopicSession session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); Topic topic = (Topic) ctx.lookup("jms/topic/neoTopic"); TextMessage msg = session.createTextMessage("Topic Test Message"); TopicPublisher publisher = session.createPublisher(topic); publisher.publish(msg); session.close(); connection.close(); } }
2、通过MessageProducer方式访问
package com.neohope.ejb.test; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; /** * Created by Hansen */ public class TestTopicBean { public static void main(String[] args) throws NamingException, JMSException { final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); jndiProperties.put(Context.PROVIDER_URL, "remote://localhost:4447"); final InitialContext ctx = new InitialContext(jndiProperties); ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); Destination destination = (Destination) ctx.lookup("jms/topic/neoTopic"); Connection connection = connectionFactory.createConnection("user001", "user001#"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); connection.start(); TextMessage msg = session.createTextMessage("Topic Test Messagee"); producer.send(msg); session.close(); connection.close(); } }