关键词不能为空

当前您在: 主页 > 英语 >

结合Spring2.0和ActiveMQ进行异步消息调用 - 完善自我,超越自我 - 七杀家园 Web开发者的博客 -

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-11 18:11
tags:

-

2021年2月11日发(作者:笋干)


结合


Spring2.0


ActiveMQ


进行异步消息调用



-


完善自我,


超越自我



-


七杀家园



Web


开发者的博客



- Powered By









Spring 2.0


之前,


Spring



JMS


的作用局限于产生消


息。这个功能(封装在



JmsTemplate


类中)当然是很好的,



但是,


它没有描述完整的


JMS


堆栈,


比如像消息的



异步




生和消耗。


JMS


堆栈缺少的这一部分已经被添加,


Spring 2.0


现在提供对消息异步消耗的完整支持。









让我们从一个例子开始。









首先我们打开


ActiveMQ




ActiveMQ

的安装路径上



bin


目录,那里 有一个



,双击执行即可。不


过要注意必 须先设置


java_home


环境变量。


ActiveMQ


默认的


服务端口是


61616










然后我们开始配置


Spring


配置文件。我起名为



首先要配置一个


ConnectionFactory


代码如下




Copy code




<bean id=










class=










<property name=


value=


</bean>





这里用 到的


ConnectionFactory


< br>ActiveMQ


提供的工厂,为


了能使用这个工厂,我 们必须在项目中添加以下几个


jar



件:



geronimo-jms_1.1_,


,


,


,


,


geronimo-j2ee- management_1.0_


以上这些


Jar

< p>
文件都存在于


ActiveMQ


安装目录的


lib


目录下,


这些可是我一个一个试验出来 的,累个半死。


。然后应该配


置一个


Q ueue


(我使用的是点对点方式)


,不过

ActiveMQ



要提供一个名字就可以自动创建队列, 因此这一步省了,呵







面就轮到


Spring


的支持类了,首先是


JmsTemplate


。这个类


提供了大量的方法简化我们对


JM S


的操作。常用的有两个,



plate102




plate


,这两个类分别支



JMS



1.02


版本和

< p>
1.1


版本。现在比较常用


的还是


1.02


版本。配置如下




Copy code





<bean id=










class=










<property name=


ref=










<property name=


value=










<property name=


value=










<property name=


ref=










<property name=


/>






</bean>




面的配置中用到了第一步配置的


connectionFactor y


以及一个


消息转换的类


messag eConverter


,这个类实现了



eConverter


接口,可以在消息发送之前和接



受之后进行消息类型转换。具体的看最后的实例代码。配置


代码如下 :




Copy code






<!-- Spring JMS SimpleConverter -->









<bean id=



clas s=


geConverter


<!-- Message Converter -->






<bean id=










class=










<property name=














<ref local=










</property>






< ;/bean>


这里还配置了发送的消息的存在时间


ti meToLive


,目标


Queue


的 名字


defaultDestinationName


,接


受消息超时时间


receiveTimeout



配置发送代码




Copy code







<!-- Message porducer -->






<bean id=










class=


gt;










<property name=


/>






</bean>


接着配置监听器,这是


Spring2.0


新增的功


能,配置如下:




Copy code








<!-- this is the Message Driven POJO (MDP) -->






<bean id=










class=


erAdapter










<constructor-arg>














<bean


















class=


/>










</constructor-arg>










<property name=


value=










<property name=


ref=






</bean>





<!-- and this is the attendant message


listener container -->






<bean id=










class=


rContainer










<property name=


ref=










<property name=


value=










<property name=


value=










<property name=


ref=






</bean>








Spring

配置监听器有很多种选择,在这里我选择这回种


MessageListenerA dapter


方法主要是因为这个方法比较灵活。


实现他只要一 个



很普通的


java


类即可,和


JMS


以及


Spr ing


的耦合度最低。


其中方法


onM essage


可以随便修改方法名,只要在配置文件


中对应的修 改就好了。








Mes sageListenerAdapter


还有一个功能就是如果处理方



(


我这里是


onMessa ge)


返回一个非空值,


它将自动返回一个

响应消息。


这个消息会返回给


JMS Reply-To< /p>


属性定义的目的



(

如果存在


)


,或者是


Messag eListenerAdapter


设置


(

如果配


置了


)


的缺省目的地;


如果没有定义目的地,


那么将产生一个


In validDestinationException


异常


(


此异常将不会只被捕获而不


处理,它将沿着调用堆栈上传


)









这样我们的配置就都完成了。接下来我们来实现对应



Java


文件



先是接口文件发送消息接口




Copy code




public interface IApmgtMessageProducer {






public abstract void sendMessage(ApmgtMessageData


messageData);


}


接受消息接口




Copy code


public interface IApmgtMessageListener {






public void onMessage(ApmgtMessageData message);


}


发消息的文件




Copy code



public class DefaultApmgtMessageProducer implements


IApmgtMessageProducer {











private JmsTemplate jmsTemplate;





public void


setJmsTemplate(JmsTemplate jmsTemplate) {










plate = jmsTemplate;






}





public void sendMessage(ApmgtMessageData


messageData) {










tAndSend(messageData);






}


}


收消息文件




Copy code




public class DefaultApmgtMessageListener implements


IApmgtMessageListener {






public void onMessage(ApmgtMessageData message) {










n(


监听到消息:

< br>






}


}


消息转换类




Copy code


public class ApmgtMessageConverter implements


MessageConverter {





private Log log =


();





private


SimpleMessageConverter converter;





public void


setConverter(SimpleMessageConverter converter) {










ter = converter;






}





public Object fromMessage(Message message)


throws JMSException, MessageConversionException {










if (message instanceof ObjectMessage) {














ObjectMessage o_message =


(ObjectMessage)message;














MessageHeader header = new MessageHeader();














(gProperty(














eiver(Property(














der(Property(














dPerson(ingProperty(


);














e(Property(














Serializable messageContent =


o_ect();














ApmgtMessageData<Serializable>


messageData = new ApmgtMessageData<Serializable>();














sageContent(messageContent);














sageHeader(header);














return messageData;










}










return null;






}





public Message toMessage(Object object, Session


session) throws JMSException, MessageConversionException {










if (object instanceof ApmgtMessageData) {














ApmgtMessageData data = (ApmgtMessageData)


object;














Message message =


age(sageContent(), session);














gProperty(


sageHeader().getId() );














Property(


sageHeader().getRecei ver());














Property(


sageHea der().getSender());














Property(


sageHeader().getType());














ing Property(


sageHeader().getSendPerson());














(


发送消息


[MessageSender ]:n


message);















return message;










} else {














return null;










}






}}


消息类文件




消息父类:




Copy code


public class ApmgtMessageData<T extends


Serializable>{





protected T messageContent;

-


-


-


-


-


-


-


-



本文更新与2021-02-11 18:11,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/637994.html

结合Spring2.0和ActiveMQ进行异步消息调用 - 完善自我,超越自我 - 七杀家园 Web开发者的博客 -的相关文章

结合Spring2.0和ActiveMQ进行异步消息调用 - 完善自我,超越自我 - 七杀家园 Web开发者的博客 -随机文章