jms Spring+ActiveMQ 5.4.2

 

 

Java Message Service(JMS)是 SUN 提出的旨在统一各种 MOM 系统接口的规范,它包含点对点(Point to Point,PTP)和发布/订阅(Publish/Subscribe,pub/sub)两种消息模型,提供可靠消息传输、事务和消息过滤等机制。 

 

JMS 支持两种截然不同的消息传送模型:PTP(即点对点模型)和Pub/Sub(即发布/订阅模型),分别称作:PTP Domain 和Pub/Sub Domain.

 

PTP(使用Queue 即队列目标)     消息从一个生产者传送至一个消费者.在此传送模型中,目标是一个队列。

 

每条消息只能发送至、并由一个消费者成功使用。如果没有已经向队列目标注册的消费者,队列将保留它收到的消息,并在某个消费者向该队列进行注册时将消息传送给该消费者.

 

Pub/Sub(使用 Topic即主题目标)消息从一个生产者传送至任意数量的消费者。在此传送模型中,目标是一个主题。每个消息可以发送至任意数量的订阅消费者。主题目标也支持持久订阅的概念。持久订阅表示消费者已向主题目标进行注册,

但在消息传送时此消费者可以处于非活动状态。当此消费者再次处于活动状态时,它将接收此信息。如果没有已经向主题目标注册的消费者,主题不保留其接收到的消息,除非有非活动消费者注册了持久订阅。

 

贴下发送和接收消息的xml配置,完整的代码从附件下载

 

发送消息xml配置

 

<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"
	xmlns:amq="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://activemq.apache.org/schema/core
		http://activemq.apache.org/schema/core/activemq-core-5.4.2.xsd" default-autowire="byName">

	<!-- 连接ActiveMQ -->
	<amq:connectionFactory id="defaultFactory" brokerURL="tcp://127.0.0.1:61616"></amq:connectionFactory>

	<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
		<property name="targetConnectionFactory" ref="defaultFactory"></property>
		<property name="sessionCacheSize" value="10"></property>
	</bean>
		
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="cachingConnectionFactory"></property>
		<property name="explicitQosEnabled" value="true" /><!-- 使 deliveryMode, priority, timeToLive设置生效-->
		<property name="deliveryPersistent" value="false" />
	</bean>
	<!-- 队列发送消息 -->
	<bean id="ptpMessageProducer" class="com.jms.producer.PtpMessageProducer">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
		<property name="queue" ref="ptpQueue"></property>
	</bean>
	<!-- 主题发送消息 -->
	<bean id="publisherMessageProducer" class="com.jms.producer.PublisherMessageProducer">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
		<property name="topic" ref="topicQueue"></property>
	</bean>
	
	<amq:queue name="ptpQueue" physicalName="ptp.queue.message"></amq:queue>
	
	<amq:topic name="topicQueue" physicalName="topic.queue.message"></amq:topic>
	
</beans>

 接收消息xml配置

 

<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"
	xmlns:amq="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://activemq.apache.org/schema/core
		http://activemq.apache.org/schema/core/activemq-core-5.4.2.xsd" default-autowire="byName">
	<description>jms 配置</description>

	<amq:connectionFactory id="defaultFactory" brokerURL="tcp://127.0.0.1:61616"></amq:connectionFactory>
											 
	<bean id="cacheConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
		<property name="targetConnectionFactory" ref="defaultFactory"></property>
		<property name="sessionCacheSize" value="10"></property>
	</bean>
	   <!--异步调用消息 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="cacheConnectionFactory"></property>
		<property name="destination" ref="ptpQueue"></property>
		<!--<property name="destination" ref="topicQueue"></property>-->
		<property name="messageListener" ref="jmsReciveListener"></property>
		<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
	</bean>
	<!-- 主题 -->
	<amq:topic id="topicQueue" physicalName="topic.queue.message"></amq:topic>
	<!-- 队列 -->
	<amq:queue id="ptpQueue" physicalName="ptp.queue.message"></amq:queue>														
	
	<bean id="jmsReciveListener" class="com.jms.receive.JmsReceiveListener"></bean>
	
</beans>

 

 

 private Destination queue ;
 private final ExecutorService threadPool = Executors.newFixedThreadPool(2);

public void sendMessage(final Object obj){
		Runnable task=new Runnable(){

			@Override
			public void run() {
				try {
					jmsTemplate.send(queue,  new MessageCreator(){

						@Override
						public Message createMessage(Session session) throws JMSException {
							ObjectMessage message = session.createObjectMessage();
				                	message.setObject(obj);
								return message;
						}
						
					});
				}catch(Exception e){
					logger.error("异常",e);
				}
			}
			
		};
		threadPool.execute(task);
	}

       public Destination getQueue() {
		return queue;
	}


	public void setQueue(Destination queue) {
		this.queue = queue;
	}

 

 

本文档旨在帮助开发者搭建STM8单片机的开发环境,并创建基于标准库的工程项目。通过本文档,您将了解如何配置开发环境、下载标准库、创建工程以及进行基本的工程配置。 1. 开发环境搭建 1.1 软件准备 IAR Embedded Workbench for STM8: 这是一个集成开发环境,具有高度优化的C/C++编译器和全面的C-SPY调试器。它为STM8系列微控制器提供全面支持。 STM8标准库: 可以从STM官网下载最新的标准库文件。 1.2 安装步骤 安装IAR: 从官网下载并安装IAR Embedded Workbench for STM8。安装过程简单,按照提示点击“下一步”即可完成。 注册IAR: 注册过程稍微繁琐,但为了免费使用,需要耐心完成。 下载STM8标准库: 在STM官网搜索并下载最新的标准库文件。 2. 创建标准库工程 2.1 工程目录结构 创建工作目录: 在自己的工作目录下创建一个工程目录,用于存放IAR生成的文件。 拷贝标准库文件: 将下载的标准库文件拷贝到工作目录中。 2.2 工程创建步骤 启动IAR: 打开IAR Embedded Workbench for STM8。 新建工程: 在IAR中创建一个新的工程,并将其保存在之前创建的工程目录下。 添加Group: 在工程中添加几个Group,分别用于存放库文件、自己的C文件和其他模块的C文件。 导入C文件: 右键Group,导入所需的C文件。 2.3 工程配置 配置芯片型号: 在工程选项中配置自己的芯片型号。 添加头文件路径: 添加标准库的头文件路径到工程中。 定义芯片宏: 在工程中定义芯片相关的宏。 3. 常见问题与解决方案 3.1 编译错误 错误1: 保存工程时报错“ewp could not be written”。 解决方案: 尝试重新创建工程,不要在原路径下删除工程文件再创建。 错误
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值