ActiveMQ 与Spring结合示例

转载:http://extrimlycold20070206121610.iteye.com/blog/510814
下面列一下步骤:
1) ActiveMQ的启动.

启动比较简单,下载ActiveMQ, 进入bin目录,启动activemq.bat.就可以了, 端口是8161.

2) 可以访问http://localhost:8161/admin进入activeMQ的console,控制台中可以手动添加和删除message, 比较方便.

3) 服务器配置

服务器用的是tomcat,主要配置是设置activeMQ的 factory与queue名称.
在 x:\tomcat-5.5\conf\context.xml 设制相关jndi

Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker"/>

<Resource name="jms/Queue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="my Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="TOOL.DEFAULT"/>

</Context> 这些基本上是比较死的东西, physicalName是指queue的名字,我用的是activeMQ的初始就有的一个。



spring中对activeMQ的配置

Java代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:amq="http://activemq.apache.org/schema/core"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans
6. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. http://activemq.apache.org/schema/core
8. http://activemq.apache.org/schema/core/activemq-core.xsd">
9.
10. <bean id="jmsConnectionFactory"
11. class="org.springframework.jndi.JndiObjectFactoryBean">
12. <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
13. </bean>
14.
15. <bean id="jmsQueue"
16. class="org.springframework.jndi.JndiObjectFactoryBean">
17. <property name="jndiName" value="java:comp/env/jms/Queue"></property>
18. </bean>
19.
20. <bean id="jmsTemplate"
21. class="org.springframework.jms.core.JmsTemplate">
22. <property name="connectionFactory" ref="jmsConnectionFactory"></property>
23. <property name="defaultDestination" ref="jmsQueue"></property>
24. </bean>
25.
26. <bean id="sender" class="message.Sender">
27. <property name="jmsTemplate" ref="jmsTemplate"></property>
28. </bean>
29.
30. <bean id="receive" class="message.Receiver"></bean>
31. <bean id="listenerContainer"
32. class="org.springframework.jms.listener.DefaultMessageListenerContainer">
33. <property name="connectionFactory" ref="jmsConnectionFactory"></property>
34. <property name="destination" ref="jmsQueue"></property>
35. <property name="messageListener" ref="receive"></property>
36. </bean>
37.
38.
39.</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
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
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">

<bean id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
</bean>

<bean id="jmsQueue"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/Queue"></property>
</bean>

<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="defaultDestination" ref="jmsQueue"></property>
</bean>

<bean id="sender" class="message.Sender">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>

<bean id="receive" class="message.Receiver"></bean>
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="destination" ref="jmsQueue"></property>
<property name="messageListener" ref="receive"></property>
</bean>


</beans>

sender与receiver的代码

Java代码
1.import javax.jms.JMSException;
2.import javax.jms.Message;
3.import javax.jms.Session;
4.
5.import org.springframework.jms.core.JmsTemplate;
6.import org.springframework.jms.core.MessageCreator;
7.
8.
9.public class Sender {
10.
11. private JmsTemplate jmsTemplate;
12.
13. public void setJmsTemplate(JmsTemplate jmsTemplate) {
14. this.jmsTemplate = jmsTemplate;
15. }
16.
17. public void send(final String text){
18. System.out.println("---Send:"+text);
19. jmsTemplate.send(new MessageCreator(){
20.
21. public Message createMessage(Session arg0) throws JMSException {
22. // TODO Auto-generated method stub
23. return arg0.createTextMessage(text);
24. }
25.
26. });
27. }
28.
29.}
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class Sender {

private JmsTemplate jmsTemplate;

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void send(final String text){
System.out.println("---Send:"+text);
jmsTemplate.send(new MessageCreator(){

public Message createMessage(Session arg0) throws JMSException {
// TODO Auto-generated method stub
return arg0.createTextMessage(text);
}

});
}

}


Java代码
1.package message;
2.
3.import javax.jms.JMSException;
4.import javax.jms.Message;
5.import javax.jms.MessageListener;
6.import javax.jms.TextMessage;
7.
8.public class Receiver implements MessageListener {
9.
10. public void onMessage(Message message) {
11. if (message instanceof TextMessage) {
12. TextMessage text = (TextMessage) message;
13.
14. try {
15. System.out.println("Receive:" + text.getText());
16. } catch (JMSException e) {
17.
18. e.printStackTrace();
19. }
20.
21. }
22. }
23.}
package message;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class Receiver implements MessageListener {

public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;

try {
System.out.println("Receive:" + text.getText());
} catch (JMSException e) {

e.printStackTrace();
}

}
}
}
主要测试了对MQ监听的功能,也就是MessageListener ,当程序启动后手动的在console里面添加了几条消息,可以监听到,另外ActiveMQ里的例子里面还有发送与消费的工具,比较好用,ConsumerTool和ProducerTool.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值