一、说明
本示例使用的ActiveMQ作为消息中间件,服务器为Glassfish,使用JMS发送消息,在MDB接收到消息之后做打印输出。
二、ActiveMQ安装配置
1、安装console
war包安装到服务器不需要特殊配置,默认监听端口为61616,可通过修改activemq.xml修改默认端口,测试使用的console版本为activemq-web-console-5.5.1,当然console可以是与客户端不同的服务器,而且我们也一般都这样使用。
2、安装ActiveMq应用端
安装rar包,如果你下载的rar包中缺少slf4j-log4j12的jar包请手工加入,与安装普通的app一样,不需要特殊配置,我采用的为activemq-rar-5.5.0.rar,不同版本对服务器可能会存在兼容性问题。
3、配置服务器
依次配置如下内容:
资源适配器(Resource Adapter Config) 资源:activemq-rar-5.5.0; 线程池ID:thread-pool-1 serverurl:tcp://localhost:61616 [如果console在其他机器,请配置对应机器的IP] 连接器连接池(Connector Connection Pools) 连接池名字:jms/ConnectionFactory 资源适配器名:activemq-rar-5.5.0 连接定义:javax.jms.ConnectionFactory 连接器资源(Connector Resources) JNDI名字:jms/ConnectionFactory 连接池名:jms/ConnectionFactory 受管对象资源(Admin Object Resources) JNDI名:jms/JMSINBOX 资源适配器名:activemq-rar-5.5.0 资源类型:javax.jms.Queue PhysicalName:jmsQueue
三、开发MDB(消息驱动Bean)
1、maven配置
<dependency> <groupId>org.glassfish</groupId> <artifactId>javax.ejb</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.jms</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
2、配置/WEB-INF/ejb-jar.xml (glassfish下必须配置)
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1" 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/ejb-jar_3_1.xsd"> <display-name>Ejb1</display-name> <enterprise-beans> <message-driven> <display-name>MyMDB</display-name> <ejb-name>MyMDB</ejb-name> <ejb-class>com.wcs.TestMDB.mdb.MyMDB</ejb-class> <messaging-type>javax.jms.MessageListener</messaging-type> <transaction-type>Container</transaction-type> <activation-config> <activation-config-property> <activation-config-property-name>DestinationType</activation-config-property-name> <activation-config-property-value>javax.jms.Queue</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>Destination</activation-config-property-name> <activation-config-property-value>jmsQueue</activation-config-property-value> </activation-config-property> </activation-config> </message-driven> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>MyMDB</ejb-name> <method-name>onMessage</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
3、配置/WEB-INF/glassfish-ejb-jar.xml (glassfish下必须配置)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE glassfish-ejb-jar PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 EJB 3.1//EN" "http://glassfish.org/dtds/glassfish-ejb-jar_3_1-1.dtd"> <glassfish-ejb-jar> <enterprise-beans> <ejb> <ejb-name>MyMDB</ejb-name> <mdb-resource-adapter> <resource-adapter-mid>activemq-rar-5.5.0</resource-adapter-mid> </mdb-resource-adapter> <mdb-connection-factory> <jndi-name>jms/ConnectionFactory</jndi-name> </mdb-connection-factory> </ejb> </enterprise-beans> </glassfish-ejb-jar>
4、消息驱动Bean代码
import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; @MessageDriven(mappedName="jms/JMSINBOX", activationConfig={ @ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge") }) public class MyMDB implements MessageListener { public void onMessage(Message arg0) { TextMessage msg = (TextMessage) arg0; try { System.out.println("Message========"+msg.getText()); } catch (JMSException e) { System.out.println("=====Server:JMSException"); e.printStackTrace(); } } }
四、开发JMS
1、maven配置
<dependency> <groupId>org.glassfish</groupId> <artifactId>javax.jms</artifactId> <version>3.0.1</version> </dependency>2、发送消息代码
jms方比较简单,只需要通过jndi获取到连接,然后把消息发送出去即可,下面只粘贴连接获取和发送部分,就不粘贴调用部分代码了。
public void sendMsg(String msg) throws NamingException { String cfJNDI = "jms/ConnectionFactory";// 配置的jndi String QUEUE_PHYSICAL_NAME = "jmsQueue";// 配置的PhysicalName InitialContext ic = new InitialContext(); //获取到连接池 ConnectionFactory connectionFactory = (ConnectionFactory) ic.lookup(cfJNDI); Connection connection = null; MessageProducer producer = null; try { //通过连接创建会话 connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建消息 TextMessage message = session.createTextMessage(); message.setText(msg); //创建消息队列 Queue queue = session.createQueue(QUEUE_PHYSICAL_NAME); //发送消息 producer = session.createProducer(queue); producer.send(message); } catch (JMSException e) { e.printStackTrace(); return; } finally { // 关闭资源 if (producer != null) { try { producer.close(); } catch (JMSException e) { e.printStackTrace(); return; } } if (connection != null) { try { connection.close(); } catch (JMSException e) { e.printStackTrace(); return; } } } }