spring + jms
来源:互联网 发布:java导出excel插件 编辑:程序博客网 时间:2024/04/23 15:22
package
jms.activemq.myexample.spring;
import
java.util.Date;
import
javax.jms.JMSException;
import
javax.jms.Message;
import
javax.jms.Session;
import
javax.jms.TextMessage;
import
org.springframework.jms.core.MessageCreator;
public
class
MyMessageCreator
implements
MessageCreator {
/**
* 消息序号
*/
private
int
msgNo;
public
MyMessageCreator(
int
no) {
this
.msgNo = no;
}
@Override
public
Message createMessage(Session session)
throws
JMSException {
TextMessage textMsg = session.createTextMessage();
textMsg.setText(
new
Date() +
"第"
+
this
.msgNo +
"条消息发出"
);
return
textMsg;
}
}
package
jms.activemq.myexample.spring;
import
javax.jms.*;
/**
* Text消息监听
*
* @author xiaochuanyu
*
*/
public
class
TextListener
implements
MessageListener {
/**
* Casts the message to a TextMessage and displays its text.
*
* @param message
* the incoming message
*/
public
void
onMessage(Message message) {
TextMessage msg =
null
;
try
{
if
(message
instanceof
TextMessage) {
msg = (TextMessage) message;
System.out.println(
"Reading message: "
+ msg.getText());
}
else
{
System.out.println(
"Message of wrong type: "
+ message.getClass().getName());
}
}
catch
(JMSException e) {
System.out.println(
"JMSException in onMessage(): "
+ e.toString());
}
catch
(Throwable t) {
System.out.println(
"Exception in onMessage():"
+ t.getMessage());
}
}
}
package
jms.activemq.myexample.spring;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
SpringJmsTestMain {
public
static
void
main(String[] args)
throws
InterruptedException {
ApplicationContext context =
new
ClassPathXmlApplicationContext(
new
String[] {
"SpringJms/SpringJms.xml"
});
SpringPublisher publisher = (SpringPublisher) context
.getBean(
"springPublisher"
);
publisher.start();
}
}
package
jms.activemq.myexample.spring;
import
javax.jms.Destination;
import
org.springframework.jms.core.JmsTemplate;
public
class
SpringPublisher {
/**
* Jms模板
*/
private
JmsTemplate template;
/**
* Topic
*/
private
Destination topic;
public
JmsTemplate getTemplate() {
return
template;
}
public
void
setTemplate(JmsTemplate template) {
this
.template = template;
}
public
Destination getTopic() {
return
topic;
}
public
void
setTopic(Destination topic) {
this
.topic = topic;
}
/**
* Start
*
* @throws InterruptedException
*/
public
void
start()
throws
InterruptedException {
int
messageCount =
10
;
while
((--messageCount) >
0
) {
sendMessage(messageCount);
Thread.sleep(
1000
);
}
}
/**
* 消息发送
*/
protected
void
sendMessage(
int
msgNO) {
this
.template.send(
this
.topic,
new
MyMessageCreator(msgNO));
}
}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- jms 连接工厂 --> <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>tcp://localhost:61616</value> </property> </bean> <!-- jms 连接池 --> <!-- <bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> <property name="connectionFactory"> <ref local="jmsFactory" /> </property> </bean> --> <!-- jms Topic --> <bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor"> <constructor-arg value="STOCKS.JAVA" /> </bean> <!-- 消息监听器 --> <bean id="myTextListener" class="jms.activemq.myexample.spring.TextListener"> </bean> <!-- jms Consumer --> <bean id="javaConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory" /> <property name="destination" ref="myTopic" /> <property name="messageListener" ref="myTextListener" /> </bean> <!-- jms 模板 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="jmsFactory" /> </property> </bean> <!-- 消息发布器 --> <bean id="springPublisher" class="jms.activemq.myexample.spring.SpringPublisher"> <property name="template"> <ref local="jmsTemplate" /> </property> <property name="topic"> <ref local="myTopic" /> </property> </bean></beans>
- spring -jms
- Spring JMS
- Spring JMS
- spring jms
- spring+jms
- spring + jms
- jms spring
- spring jms
- Spring JMS
- JMS + Spring
- Spring JMS
- Spring JMS 消息处理
- jms+spring配置
- Spring整合JMS
- Spring+JMS例子
- jms with spring
- jms with spring 2
- Spring Jms入门实例
- love to seven year
- 昌吉1384181490099
- uva Potentiometers
- c语言变量存储方式
- 51单片机STC89C52 定时器(中断方式)
- spring + jms
- 东港1384181511927
- 慈溪1384181506824
- 衡阳1384181514708
- tasklist
- 如何登录Google美国服务器
- 龙泉1384181546130
- 每天两道acm 题解 (具体参考ACM国际大学生程序设计竞赛题解)
- 洮南1384181552074