公司用过基于websphere上的JMS的应用,主要用的是QUEUE,关于topic的还没有用到过,但是,国内许多公司用的开源的比较多一点,因为成本比较低,现阶段是SSH成风,所以spring的JMS(MQ)的应用是要学习一下,今天学习的是spring上应用activeMQ,都是开源的,呵呵.
下面列一下步骤:
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
<?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的配置
<?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的代码
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);
}
});
}
}
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.
附件有个可以跑的例子.
分享到:
相关推荐
ActiveMQ与Spring整合示例Demo,ActiveMQ安装在linux系统中
activemq+spring demo 简单示例。安装activemq 启动,运行Test
activemq+spring demo 简单示例
activeMQ+spring+springmvc整合示例
C#下使用消息中间件 ActiveMQ 和Spring.NET框架开发示例
Spring整合JMS(activeMQ)的示例,开发环境为eclipse+maven
activeMQ和Spring集成的 SpringJMS示例代码,适合新手,老鸟勿喷请绕道。ps:为什么传个资源一定要设置下载分呢
此项目比较完整的演示了ActiveMQ与Spring整合的相关示例,由于机器限制暂时做的是单项目自测,如有需要可以自己完善。
主要介绍了ActiveMQ结合Spring收发消息的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ActiveMQ、Apollo、Spring Jms 整合示例。 本实例演示环境为:ActiveMQ 5.10 、 Windows 7 X64 、jdk1.7、maven 3.0.5 本实例演示环境为:apollo-1.7 、 Windows 7 X64 、jdk1.7、maven 3.0.5 安装ActiveMQ 1. ...
本项目基于Spring boot这一平台,整合流行的开源消息队列中间件ActiveMQ,实现一个向ActiveMQ添加和读取消息的功能。分别实现生产者-消费者模式和发布-订阅模式,作为java编程发送消息和消费消息的基础示例。 源码...
spring+activemq,示例工程,演示JMS消息通信。。。。。。。
Spring 和 activemq 搭建JMS开发系统示例,学习此示例可以让你快速了解JMS,这种方式对服务器没有依赖。
spring boot activemq集成示例,包含queue和topic消息的发送、接收,连接池的支持。
请将本maven项目引入你自己的maven项目中(在你自己的pom.xml文件中配置这个项目的gourp和id以及版本号),通过模块化导入,注意把spring-activeMQ.xml加载到容器当中,运行tomcat启动项目,即可看到效果。
SpringIntegrationMQ
spring-mq完美整合示例,在工作中用的activeMQ,这个是spring整合mq
Spring JMS with ActiveMQ – 发送和接收消息示例。 此代码假装是将 ActiveMQ 与 JMS Spring Beans 一起使用的简单概念证明。 这个想法是查找 Spring 上下文来处理 JMS bean。 配置 您需要配置一个名为 BATCH_CONF ...
主要介绍了Spring Boot ActiveMQ连接池配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下