jms是大家在项目中经常用到的技术,而activemq又是开源的jms产品中比较优秀的。在应用jms处理相关业务时,大家都是构造消息,然后发送到队列,最后用message监听器监听到消息,对消息进行分析处理。在这个过程中,有两步是比较麻烦和重复的,那就是构造消息和拆解消息。并且这样与面向对象的思想很是违背。如果这样做显然是面向消息数据的,而不是面向对象的。基于以上原因,我向大家介绍一个我在项目中经过实践的开源框架:lingo.
lingo在jms中起的主要作用就是对消息的封装,它让你可以不必关心消息的构造和拆解,而只需关心你本身的业务逻辑。我将举一个例子,分别用activemq直接实现和用lingo实现。在这个例子中我用到了spring framework.
=================================hello.java===========================
import java.io.serializable;
/**hello.java用来传递java对象
* author: cjp
* date: 2005-11-8
* time: 22:24:02
*/
public class hello implements serializable {
private string id;
private hello hello;
private pointlist pointlist;
public string getid() {
return id;
}
public void setid(string id) {
this.id = id;
}
public hello gethello() {
return hello;
}
public void sethello(hello hello) {
this.hello = hello;
}
}
=========================springtest .java========================
import org.springframework.jms.core.jmstemplate;
import org.springframework.jms.core.messagecreator;
import org.springframework.test.abstractdependencyinjectionspringcontexttests;
import javax.jms.*;
/**
*发送jms消息
*/
public class springtest extends abstractdependencyinjectionspringcontexttests
{
protected string[] getconfiglocations()
{
return new string[]{"file:d://wosame//test//com//wosame//room//jms//jms.xml"};
}
public void testsendmessage() throws exception
{
jmstemplate jmstemplate = (jmstemplate) applicationcontext.getbean("jmstemplate");
jmstemplate.send(new messagecreator()
{
public message createmessage(session session) throws jmsexception
{
objectmessage message=session.createobjectmessage();
hello hello=new hello();
hello.setid("test");
message.setobject(hello);
return message;
}
});
}
}
================================hellomdp .java==================================
/**
处理jms消息
*/
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import javax.jms.*;
public class hellomdp implements messagelistener
{
protected log log = logfactory.getlog(hellomdp.class);
public void onmessage(message message)
{
try
{
objectmessage objmessage = (objectmessage) message;
hello hello= (hello) objmessage.getobject();
system.out.println("hello.getid() = " + hello.getid());
} catch (jmsexception e)
{
log.error("parse failed", e);
}
}
}
================================jms.xml==================================
<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public "-//spring//dtd bean//en"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--嵌入式的jms连接,也就是跟随jvm一起启动,可以参看activemq的文档-->
<bean id="connectionfactory" class="org.activemq.activemqconnectionfactory"> <property name="brokerurl" value="vm://localhost"/>
<property name="useembeddedbroker" value="true"/>
</bean>
<!--消息监听器,也就是消息的具体的处理器-->
<bean id="hellomdp" class="hellomdp"/>
<!--jms监听需要jta容器的支持-->
<bean id="activemqcontainer" class="org.activemq.jca.jcacontainer">
<property name="workmanager">
<bean id="workmanager" class="org.activemq.work.springworkmanager"/>
</property>
<property name="resourceadapter">
<bean id="activemqresourceadapter" class="org.activemq.ra.activemqresourceadapter">
<property name="serverurl" value="vm://localhost"/>
</bean>
</property>
</bean>
<!--消息的消费者,也就是将监听器与具体的队列关联-->
<bean id="helloqueueconsumer" factory-method="addconnector" factory-bean="activemqcontainer">
<property name="activationspec">
<bean class="org.activemq.ra.activemqactivationspec">
<property name="destination" value="hello.queue"/>
<property name="destinationtype" value="javax.jms.queue"/>
</bean>
</property>
<property name="ref" value="hellomdp"/>
</bean>
<!--spring的jms template,用来发送jms消息到指定的队列-->
<bean id="jmstemplate" class="org.springframework.jms.core.jmstemplate">
<property name="defaultdestinationname" value="hello.queue"/>
<property name="connectionfactory" ref="connectionfactory"/>
</bean>
</beans>
闽公网安备 35060202000074号