1. spring中的配置文件
<?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:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">
<!-- *************************** 连接openMQ server ******************************** -->
<bean id="connectionfactoryfactory" class="com.fg114.customercenter.jms.factory.OpenMqConnectionFactory">
<property name="properties">
<props>
<prop key="imqAddressList">localhost:7676</prop>
</props>
</property>
</bean>
<bean id="connectionfactory" factory-bean="connectionfactoryfactory" factory-method="createConnectionFactory"/>
<bean id="springconnectionfactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="connectionfactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="springconnectionfactory" />
</bean>
<!-- *************************** topic 和 queue TOPIC通道可以有消息消费者, QUEUE只能一个消息消费者******************************** -->
<bean name="mytopic1" class="com.sun.messaging.Topic">
<constructor-arg type="java.lang.String" value="mytopic1"/>
</bean>
<bean name="mytopic2" class="com.sun.messaging.Topic">
<constructor-arg type="java.lang.String" value="mytopic2"/>
</bean>
<bean name="myqueue" class="com.sun.messaging.Queue">
<constructor-arg type="java.lang.String" value="myqueue"/>
</bean>
<!--************************* 有几个MessageListener既有几个MessageListenerContainer **********-->
<bean name="topicListenerMytopic1" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="messageReceiver"/>
<property name="messageConverter" ref="springMessageConverter" />
<property name="defaultListenerMethod" value="receiveTopic"/>
</bean>
<bean id="topicListenerContainerMytopic1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="springconnectionfactory" />
<property name="destination" ref="mytopic1" />
<property name="messageListener" ref="topicListenerMytopic1" />
</bean>
<bean name="topicListenerMytopic2" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="messageReceiver"/>
<property name="messageConverter" ref="springMessageConverter" />
<property name="defaultListenerMethod" value="receiveTopic"/>
</bean>
<bean id="topicListenerContainerMytopic2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="springconnectionfactory" />
<property name="destination" ref="mytopic2" />
<property name="messageListener" ref="topicListenerMytopic2" />
</bean>
<bean name="queueListenerMyqueue1" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="messageReceiver"/>
<property name="messageConverter" ref="springMessageConverter" />
<property name="defaultListenerMethod" value="receiveTopic"/>
</bean>
<bean id="queueListenerContainerMyqueue1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="springconnectionfactory" />
<property name="destination" ref="myqueue"/>
<property name="messageListener" ref="queueListenerMyqueue1" />
</bean>
<bean name = "springMessageConverter" class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</beans>
2. 接收
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.jms.receive;
import com.test.jms.model.Customer;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Topic;
import org.springframework.stereotype.Component;
/**
*
* @author ann
*/
@Component(value = "messageReceiver")
public class MessageReceiver {
public void receive(String msg) {
System.out.println(msg);
}
public void receive(Serializable msg) {
if(msg instanceof Customer){
Customer c = (Customer) msg;
System.out.println("c = " + c.getName());
}else {
String s = (String) msg;
System.out.println("s = " + s);
}
}
public void receiveTopic(String msg){
//String s = (String) msg;
System.out.println("s = " + msg);
}
}
3.发送
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fg114.test.jms.service.impl;
import java.io.Serializable;
import javax.jms.Destination;
import javax.jms.Topic;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session ;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
*
* @author ann
*/
//@Component
public class MessageSendServiceImpl {
// @Resource(name = "jmsTemplate")
// private JmsTemplate jmsTemplate;
//
// @Resource(name = "mytopic1")
// private Destination desctination;
@Override
public void sendString(String queueName, final String msg) {
// jmsTemplate.send(queueName, new MessageCreator() {
// public Message createMessage(Session session) throws JMSException {
// return session.createTextMessage(msg);
// }
// });
}
@Override
public void sendObject(String queueName, final Serializable obj) {
// jmsTemplate.send(queueName, new MessageCreator() {
// public Message createMessage(Session session) throws JMSException {
// return session.createObjectMessage (obj);
// }
// });
}
public void sendTopic(final String message){
// jmsTemplate.send(desctination, new MessageCreator() {
// public Message createMessage(Session session) throws JMSException {
// return session.createTextMessage(message);
// }
// });
}
}
posted on 2009-04-24 10:56
冰是没有未来的,因为它的永恒 阅读(1453)
评论(0) 编辑 收藏 所属分类:
java