异步的,服务端可以发送信息到一个客户端,客户端不需要为了收到信息而请求信息。
l JMS client 客户端 用于发送和接收信息,通常是用在java程序中用java编写的
Messagesl 用于在客户端信息通信的对象
Administered Objects 由JMSl provider为了client创建的对象,通常是connectionFactory和destination
JMS通信方式分为点对点通信,和发布/订阅方式
点对点的消息发送方式主要建立在 Message Queue,Sender,reciever上,Message Queue 存贮消息,Sneder 发送消息,receive接收消息,具体的信息就是 Sender Client发送Message 到Queue ,而 receiver Cliernt 从Queue中接收消息和发送消息已接受到Quere,确认消息接收。在使用点对点方式时需要注意,一条消息只有一个接收端,消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行
发布/订阅方式用于多接收客户端的方式
作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。
1.多种语言和协议编写客户端
语言: Java, C, C++, C#, Ruby, Perl, Python, PHP
应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
2.完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
3.对Spring的支持,ActiveMQ可以很容得内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
4.通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,
可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
5.支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
6.支持通过JDBC和journal提供高速的消息持久化
7.从设计上保证了高性能的集群,客户端-服务器,点对点
8.支持Ajax
9.支持与Axis的整合
10.可以很容易得调用内嵌JMS provider,进行测试
Administered Objects:ConnectionFactory,Destination
Connection;
Session
Message Producers/Message Cosumers
Messages
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>
import java.util.Date;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.util.IndentPrinter;
* A simple tool for publishing messages
*
* @version $Revision: 1.2 $
*/
public class ProducerTool {
private int messageCount = 10;
private long sleepTime = 0L;
private boolean verbose = true;
private int messageSize = 255;
private long timeToLive;
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "TOOL.DEFAULT";
private boolean topic = false;
private boolean transacted = false;
private boolean persistent = false;
ProducerTool producerTool = new ProducerTool();
String[] unknonwn = CommnadLineSupport.setOptions(producerTool, args);
if( unknonwn.length > 0 ) {
System.out.println("Unknown options: "+Arrays.toString(unknonwn));
System.exit(-1);
}
producerTool.run();
}
Connection connection=null;
try {
System.out.println("Connecting to URL: " + url);
System.out.println("Publishing a Message with size " + messageSize+ " to " + (topic ? "topic" : "queue" + ": " + subject);
System.out.println("Using " + (persistent ? "persistent" : "non-persistent" + " messages" ;
System.out.println("Sleeping between publish " + sleepTime + " ms" ;
if (timeToLive != 0) {
System.out.println("Messages time to live " + timeToLive + " ms" ;
}
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = connectionFactory.createConnection();
connection.start();
// Create the session
Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
if (topic) {
destination = session.createTopic(subject);
} else {
destination = session.createQueue(subject);
}
// Create the producer.
MessageProducer producer = session.createProducer(destination);
if (persistent) {
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
} else {
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
if (timeToLive != 0)
producer.setTimeToLive(timeToLive);
// Start sending messages
sendLoop(session, producer);
// Use the ActiveMQConnection interface to dump the connection stats.
ActiveMQConnection c = (ActiveMQConnection) connection;
c.getConnectionStats().dump(new IndentPrinter());
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Throwable ignore) {
}
}
}
throws Exception {
.createTextMessage(createMessageText(i));
String msg = message.getText();
if (msg.length() > 50) {
msg = msg.substring(0, 50) + "...";
}
System.out.println("Sending message: " + msg);
}
if (transacted) {
session.commit();
}
StringBuffer buffer = new StringBuffer(messageSize);
buffer.append("Message: " + index + " sent at: " + new Date());
if (buffer.length() > messageSize) {
return buffer.substring(0, messageSize);
}
for (int i = buffer.length(); i < messageSize; i++) {
buffer.append(' ');
}
return buffer.toString();
}
public void setPersistent(boolean durable) {
this.persistent = durable;
}
public void setMessageCount(int messageCount) {
this.messageCount = messageCount;
}
public void setMessageSize(int messageSize) {
this.messageSize = messageSize;
}
public void setPassword(String pwd) {
this.password = pwd;
}
public void setSleepTime(long sleepTime) {
this.sleepTime = sleepTime;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setTimeToLive(long timeToLive) {
this.timeToLive = timeToLive;
}
public void setTopic(boolean topic) {
this.topic = topic;
}
public void setQueue(boolean queue) {
this.topic = !queue;
}
public void setTransacted(boolean transacted) {
this.transacted = transacted;
}
public void setUrl(String url) {
this.url = url;
}
public void setUser(String user) {
this.user = user;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
}