如果用过JMS的话,会发现它类似写JDBC或JCA代码。它有创建或检索JMS资源的样板代码,每当你需要编写一个新类来发送或接受消息时,都得重复编写那个样本代码。下面列出了传统JMS实现涉及的步骤:
1、创建JNDI初始上下文context;
2、从JNDI上下文获得队列连接工厂;
3、从队列连接工厂取得队列Queue;
4、创建一个Session对象;
5、创建一个发送或接受对象;
6、利用第5部创建的发送或接受对象发送或接受消息;
7、处理完消息后,关闭所有JMS资源。
在JBOSS中配置:
<?xml version="1.0" encoding="UTF-8"?> <!-- $Id: mail-service.xml 62350 2007-04-15 16:50:12Z [email protected] $ --> <server> <!-- ==================================================================== --> <!-- Mail Connection Factory --> <!-- ==================================================================== --> <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=longgangbai"> <attribute name="JNDIName">queue/longgangbai</attribute> <depends optional-attribute-name="DestinationManager" >jboss.mq:service=DestinationManager</depends> </mbean> </server>
Spring中JMS的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 设置消息服务的初始化参数信息 --> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial"> org.jnp.interfaces.NamingContextFactory </prop> <prop key="java.naming.provider.url"> jnp://localhost:1099 </prop> <prop key="java.naming.factory.url.pkgs"> org.jboss.naming:org.jnp.interfaces </prop> </props> </property> </bean> <!-- 创建消息服务的连接工厂的信息 JMS1.02 TopicConnectionFactory 和QueueConnectionFactory JMS1.1 TopicConnectionFactory 和QueueConnectionFactory合并为ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>ConnectionFactory</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"/> </property> </bean> <!-- 创建JMS的目的地信息 --> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>queue/longgangbai</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"/> </property> </bean> <!-- 创建JMS发送信息的模板的对象 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="connectionFactory"/> </property> <property name="defaultDestination"> <ref local="destination"/> </property> </bean> </beans>
Spring中JMS的代码如下:
package com.easyway.jboss.jms.ptp.spring.service; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; /** * Spring中JMS的简单应用 * @author longgangbai * */ public class JmsTemplate11 { private static final Log log=LogFactory.getLog(JmsTemplate11.class); public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); JmsTemplate jt=(JmsTemplate)ctx.getBean("jmsTemplate"); jt.send(new MessageCreator(){ @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("the spring queue /TestQueue"); } }); log.info("发送信息成功!"); } }
客户端的配置:applicationContext-jms-receive.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 设置消息服务的初始化参数信息 --> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial"> org.jnp.interfaces.NamingContextFactory </prop> <prop key="java.naming.provider.url"> jnp://localhost:1099 </prop> <prop key="java.naming.factory.url.pkgs"> org.jboss.naming:org.jnp.interfaces </prop> </props> </property> </bean> <!-- 创建消息服务的连接工厂的信息 JMS1.02 TopicConnectionFactory 和QueueConnectionFactory JMS1.1 TopicConnectionFactory 和QueueConnectionFactory合并为ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>ConnectionFactory</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"/> </property> </bean> <!-- 创建JMS的目的地信息 --> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>queue/longgangbai</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"/> </property> </bean> <!-- 创建JMS发送信息的模板的对象 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="connectionFactory"/> </property> <property name="defaultDestination"> <ref local="destination"/> </property> </bean> <!-- 定义消息的监听器的实现 --> <bean id="messageListener" class="com.easyway.jboss.jms.ptp.spring.service.SpringJMSMessageListener"> <property name="jmsTemplate"> <ref bean="jmsTemplate"/> </property> </bean>
package com.easyway.jboss.jms.ptp.spring.service;
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.jms.core.JmsTemplate; /** * Spring JMS 中消息监听的事件的处理 * * @author longgangbai * */ public class SpringJMSMessageListener implements MessageListener ,InitializingBean,DisposableBean{ private static final Log log=LogFactory.getLog(SpringJMSMessageListener.class); private JmsTemplate jmsTemplate; private Connection conn; public JmsTemplate getJmsTemplate() { return jmsTemplate; }
public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; }
public Connection getConn() { return conn; }
public void setConn(Connection conn) { this.conn = conn; }
/** * 处理消息的方法 */ @Override public void onMessage(Message message) { if (message instanceof TextMessage) { try { System.out.println(((TextMessage) message).getText()); } catch (JMSException ex) { throw new RuntimeException(ex); } } else { throw new IllegalArgumentException("Message must be of type TextMessage"); } }
/** * 初始化的连接的操作 */ @Override public void afterPropertiesSet() throws Exception { ConnectionFactory conFactory=jmsTemplate.getConnectionFactory(); Destination destination=jmsTemplate.getDefaultDestination(); this.conn=conFactory.createConnection("guest", "guest"); Session session=conn.createSession(false, QueueSession.AUTO_ACKNOWLEDGE); MessageConsumer mc=session.createConsumer(destination); mc.setMessageListener(this); conn.start(); }
/** * 销毁连接的操作 */ @Override public void destroy() throws Exception { conn.close(); }
}