使用Spring的MDP,我们可以与JMS服务提供者通信,向队列或是主题发送或消费JMS消息,下面是两个示例程序,分别是与队列和主题通信,两个程序大同小异,主要是配置上有些许区别,其中Weblogic中的JMS相关配置部分请见“
Weblogic9.2中JMS的相关配置”。
为了减少体积,这两个程序去除了库,这些库是commons-logging-1.0.4.jar,dom4j-1.6.1.jar,log4j-1.2.14.jar,mysql-connector-java-5.0.6-bin.jar,spring.jar,weblogic.jar,其中mysql-connector-java-5.0.6-bin.jar不是必要的,因为与数据库连接的部分程序已经删除。大家可以自行寻找这些jar包加入工程中。
与队列通信的程序下载地址如下:
http://www.blogjava.net/Files/heyang/SpringJMS_Topic20090924103204.rar
此程序的Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- jndiTemplate 配置 -->
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://localhost:7001
</prop>
<prop key="java.naming.factory.url.pkgs">
weblogic.jndi.factories
</prop>
</props>
</property>
</bean>
<!-- JMS队列 配置 -->
<bean id="destination"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>MyJmsQueue</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-- JMS连接工厂 配置 -->
<bean id="connectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>MyJmsConnectionFactory</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-- jmsTemplate 配置 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory" />
</property>
<property name="defaultDestination">
<ref local="destination" />
</property>
</bean>
<!-- JMS 消息发送者 配置-->
<bean id="jmsQueueSender"
class="com.heyang.sender.JmsQueueSender">
<property name="connectionFactory" ref="connectionFactory" />
<property name="queue" ref="destination" />
</bean>
<!-- JMS 消息监听容器 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="concurrentConsumers" value="1" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- Message Driven POJO (MDP 消息驱动POJO) -->
<bean id="messageListener" class="com.heyang.receiver.JmsQueueListener" />
</beans>
消息发送者源码:
package com.heyang.sender;
import java.io.File;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessageCreator;
public class JmsQueueSender{
private JmsTemplate jmsTemplate;
private Queue queue;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate102(cf, false);
}
public void setQueue(Queue queue) {
this.queue = queue;
}
public void sendMsg(final String msg) {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
public static void main(String[] args) throws Exception{
ApplicationContext appContext = new ClassPathXmlApplicationContext("bean.xml");
JmsQueueSender jmsSender = (JmsQueueSender)appContext.getBean("jmsQueueSender");
jmsSender.sendMsg(readProvinceMsgFromXMl("msg.xml"));
System.out.println("消息发送完毕");
}
/**
* 从XML文件中读取文本
* @param xmlFile
* @throws DocumentException
*/
public static String readProvinceMsgFromXMl(String xmlFile) throws DocumentException{
SAXReader reader=new SAXReader();
Document document=reader.read(new File(xmlFile));
Element root=document.getRootElement();
return root.asXML();
}
}
消息消费者源码:
package com.heyang.receiver;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class JmsQueueListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
String msg=((TextMessage) message).getText();
System.out.print("已经收到消息:"+msg);
Document document=DocumentHelper.parseText(msg);
Element root=document.getRootElement();
System.out.println(root.asXML());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
与主题通信的程序下载地址如下:
http://www.blogjava.net/Files/heyang/SpringJMS_Queue20090924103214.rar
此程序的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- jndiTemplate 配置 -->
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://localhost:7001
</prop>
<prop key="java.naming.factory.url.pkgs">
weblogic.jndi.factories
</prop>
</props>
</property>
</bean>
<!-- JMS主题 配置 -->
<bean id="destination"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>MyJmsTopic</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-- JMS连接工厂 配置 -->
<bean id="connectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>MyJmsConnectionFactory</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-- jmsTemplate 配置 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory" />
</property>
<property name="defaultDestination">
<ref local="destination" />
</property>
</bean>
<!-- JMS 消息发送者 配置-->
<bean id="jmsSender"
class="com.heyang.sender.JmsSender">
<property name="connectionFactory" ref="connectionFactory" />
<property name="topic" ref="destination" />
</bean>
<!-- JMS 消息监听容器 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="concurrentConsumers" value="2" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- Message Driven POJO (MDP 消息驱动POJO) -->
<bean id="messageListener" class="com.heyang.receiver.JmsListener" />
</beans>
消息发布者源码:
package com.heyang.sender;
import java.io.File;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessageCreator;
public class JmsSender{
private JmsTemplate jmsTemplate;
private Topic topic;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate102(cf, true);
}
public void setTopic(Topic queue) {
this.topic = queue;
}
public void sendMsg(final String msg) {
this.jmsTemplate.send(this.topic, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
public static void main(String[] args) throws Exception{
ApplicationContext appContext = new ClassPathXmlApplicationContext("bean.xml");
JmsSender jmsSender = (JmsSender)appContext.getBean("jmsSender");
jmsSender.sendMsg(readProvinceMsgFromXMl("msg.xml"));
System.out.println("消息发送完毕");
}
/**
* 从XML文件中读取文本
* @param xmlFile
* @throws DocumentException
*/
public static String readProvinceMsgFromXMl(String xmlFile) throws DocumentException{
SAXReader reader=new SAXReader();
Document document=reader.read(new File(xmlFile));
Element root=document.getRootElement();
return root.asXML();
}
}
消息订阅者源码:
package com.heyang.receiver;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class JmsListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
String msg=((TextMessage) message).getText();
System.out.print("已经收到消息:"+msg);
Document document=DocumentHelper.parseText(msg);
Element root=document.getRootElement();
System.out.println(root.asXML());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}