`

8.开发消息驱动Bean

阅读更多

1.配置消息服务

<?xml version="1.0" encoding="utf-8"?>

<server>
	<mbean code="org.jboss.mq.server.jmx.Queue"
		name="jboss.mq.destination:service=Queue,name=EJBQueue">
		<attribute name="JNDIName">queue/EJBQueue</attribute>
		<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
	</mbean>
	
	<mbean code="org.jboss.mq.server.jmx.Topic"
		name="jboss.mq.destination:service=Topic,name=EJBTopic">
		<attribute name="JNDIName">topic/EJBTopic</attribute>
		<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
	</mbean>
</server>

 注:文件名必须以-service.xml结尾,否则不会被当作服务配置文件解析的。

2.点对点的消息发送

package com.taoist.ejb.bean;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MessageBean {
	public static void main(String[] args) {
		try {
			InitialContext ic = new InitialContext();
			QueueConnectionFactory factory = 
                                                        (QueueConnectionFactory) ic.lookup("ConnectionFactory");
			QueueConnection conn = factory.createQueueConnection();
			//第一个参数指明是否需要事务,第二个参数指明消息接收的确认模式
			QueueSession session = conn.createQueueSession(false, 
                                                                                         QueueSession.AUTO_ACKNOWLEDGE);
			Destination destination = (Destination) ic.lookup("queue/EJBQueue");
			MessageProducer producer = session.createProducer(destination);
			producer.send(session.createTextMessage("Hello message!"));
			session.close();
			conn.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
 
package com.taoist.ejb.bean;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = { 
	@ActivationConfigProperty(propertyName = "destinationType"
		, propertyValue = "javax.jms.Queue"),
	@ActivationConfigProperty(propertyName = "destination"
		, propertyValue = "queue/EJBQueue"),
	@ActivationConfigProperty(propertyName = "acknowledgeMode"
		, propertyValue = "Auto-acknowledge")
})
public class MessageDriverBean implements MessageListener {

	@Override
	public void onMessage(Message message) {
		TextMessage msg = (TextMessage)message;
		try {
			System.out.println(msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}

 3.发布/订阅模式的消息发送

package com.taoist.ejb.bean;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueSession;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicSender {

	public static void main(String[] args) {
		try {
			InitialContext ic = new InitialContext();
			TopicConnectionFactory factory = (TopicConnectionFactory) ic.lookup("ConnectionFactory");
			TopicConnection conn = factory.createTopicConnection();
			//第一个参数指明是否需要事务,第二个参数指明消息接收的确认模式
			TopicSession session = conn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			Destination destination = (Destination) ic.lookup("topic/EJBTopic");
			MessageProducer producer = session.createProducer(destination);
			producer.send(session.createTextMessage("Hello message! Topic!"));
			session.close();
			conn.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
 
package com.taoist.ejb.bean;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = { 
	@ActivationConfigProperty(propertyName = "destinationType"
		, propertyValue = "javax.jms.Topic"),
	@ActivationConfigProperty(propertyName = "destination"
		, propertyValue = "topic/EJBTopic")
})
public class ReceiveBean implements MessageListener {

	@Override
	public void onMessage(Message message) {
		TextMessage msg = (TextMessage) message;
		try {
			System.out.println("ReceiveBean:" + msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

 

package com.taoist.ejb.bean;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = { 
	@ActivationConfigProperty(propertyName = "destinationType"
		, propertyValue = "javax.jms.Topic"),
	@ActivationConfigProperty(propertyName = "destination"
		, propertyValue = "topic/EJBTopic")
})
public class ReceiveBean2 implements MessageListener {

	@Override
	public void onMessage(Message message) {
		TextMessage msg = (TextMessage) message;
		try {
			System.out.println("ReceiveBean2:" + msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

            可以看到,发布一条信息,两个消息驱动Bean都接收到了。

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics