今天看书看了下JMS,初步有了个了解,具体的JMS知识还需要进一步的深入学习。
J2EE中,JMS(Java Message System)提供了一种异步处理机制的实现。JMS通过异步的、非阻塞的消息传递,将消息的生产者和使用者松散的联系在一起。对于使用者,它无所谓是谁产生了消息或者是在何时产生的。这就能够建立一种动态的、灵活的可靠的系统。所谓可靠,因为JMS将每个消息都保存起来,只有确保本消息处理后才会完全放弃。否则,将会反复提交处理。这种可靠的机制使得JMS能够成功的在证券、银行系统中得到广泛应用。
JMS中的消息类型有两种:Topic和Queue。Topic的操作是使用发布/订阅(publish/subscribe)的方式;Queue的操作是点对点(ponit to point)的方式。
·publish/subscribe:发布者(publisher)发布消息到Topic,订阅者(subsribe)从Topic订阅消息,订阅者的数量是不受限制的。
· ponit to point:点对点传输消息,建立在消息队列Queue的基础上,每个消息使用者只对应一个消息队列,不像publish/subscribe那样可以有很多消息使用者。
JMS在消息到达消息使用者,有两种——同步和异步。
·同步是指消息使用者明确地主动地调用从Queue或Topic中得到消息,一直进行循环直至一个消息到达,或者也可以设定超时时间。很明显这个方法是比较耗费CPU资源的。
·异步接受是指消息到达时,主动通知消息使用者,消息使用者可以实现message listener接口。这样每当消息到达时,JMS provider 会通过调用这个listener的onMessage方法来传输这个消息。
参考spring的资料,写了个Spring+JMS的例子。
1.建立工程spring_jms,导入工程相应的包,所用的包都是ActiveMQ自带的,在apache-activemq-5.2.0\lib\optional下可以找到。
2.applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<!-- 配置JMS模版 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- Set the Queue Name -->
<constructor-arg index="0" value="HelloWorldQueue"/>
</bean>
</beans>
3.消息发送程序HelloWorldSender
package com.springjms;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
/**
*
* @author Wind
*
*/
public class HelloWorldSender {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
Destination destination = (Destination) ctx.getBean("destination");
template.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello World,ActiveMQ Spring JMS Message!");
}
});
System.out.println("JMS Message Sent");
}
}
4.消息接收程序HelloWorldReceiver
package com.springjms;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
*
* @author Wind
*
*/
public class HelloWorldReceiver {
public static void main(String[] args) throws JMSException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
Destination destination = (Destination) ctx.getBean("destination");
while (true) {
TextMessage txtmsg = (TextMessage) template.receive(destination);
if (null != txtmsg){
System.out.println("Received Message: " + txtmsg.getText());
}else{
break;
}
}
}
}
5.启动ActiveMQ,先运行发送者HelloWorldSender,然后运行HelloWorldSenderReceiver,控制台输出如下:
Received Message: Hello World,ActiveMQ Spring JMS Message!
6.继续运行HelloWorldSender发现,接收端接收一条消息后没有退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
7.哈哈,这个也很简单,继续看书,接下来弄个JMS本地事务的例子。
- 大小: 17.7 KB
分享到:
相关推荐
NULL 博文链接:https://elim.iteye.com/blog/1983532
Spring整合JMS,基于ActiveMQ实现收发消息
spring整合JMS-居于ActiveMQ实现
Spring+JMS + Mysql这是一个很值得学习的例子,已经封装成了一个对象。。,Spring+JMS + Mysql
spring3.0整合了activemQ和jms可以发送信息和接收消息
spring集成JMS
整合JMS Spring
JMS实例,整合spring,含jar,完整可运行
Spring整合JMS(activeMQ)的示例,开发环境为eclipse+maven
NULL 博文链接:https://elim.iteye.com/blog/1900937
详细内容: SpringJMS整合ActiveMQ.doc 详细说明文档 apache-activemq-5.8.0-bin.zip ActiveMQ安装包 JMSTest.rar MyEclipse8.5下web工程
activemq与spring整合发送jms消息入门实例 jar 包和实例代码都在压缩包里了
——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!
本人开发的spring jms项目,已经上线近一年了,这里是我项目jms配置文件,使用的是spring jms 整合weblogic jms。如果真的需要,请咨询我,并且附上我上传的这个配置文件,附近中没有带有这个文件,一律不作任何回答...
java消息中间件学习-Spring集成JMS连接ActiveMQ demo
这是本人搭建的Spring框架,集成了Weblogic JMS,测试已经完成。现提供给大家下载学习。
本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。...3) Spring整合activemq配置 4) 定义消息发布者(生产者) 5) 定义消息订阅者(消费者) 6) Spring mvc配置 7) 实例测试
activeMQ和Spring集成的 SpringJMS示例代码,适合新手,老鸟勿喷请绕道。ps:为什么传个资源一定要设置下载分呢
本篇文章主要介绍了spring整合JMS实现同步收发消息(基于ActiveMQ的实现),具有一定的参考价值,感兴趣的小伙伴们可以参考一下