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 >