JMS Api Demo

jms.JMSFactory
Java代码 复制代码 收藏代码
  1. package jms;
  2. import javax.jms.TopicConnectionFactory;
  3. import org.apache.activemq.ActiveMQConnectionFactory;
  4. public class JMSFactory {
  5. private static ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
  6. public static TopicConnectionFactory getActiveMQConnectionFactory(){
  7. return activeMQConnectionFactory;
  8. }
  9. }
package jms;
import javax.jms.TopicConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSFactory {
	private static ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
	
	public static TopicConnectionFactory getActiveMQConnectionFactory(){
		return activeMQConnectionFactory;
	}
}

jms.JMSMessageActor
Java代码 复制代码 收藏代码
  1. package jms;
  2. import javax.jms.*;
  3. public abstract class JMSMessageActor {
  4. protected String name=null;
  5. protected String defaultQueueName="defalut-queue";
  6. protected String defaultTopicName="defalut-topic";
  7. protected final int DESTIONATION_TYPE_TOPIC=1;
  8. protected final int DESTIONATION_TYPE_QUEUE=2;
  9. public JMSMessageActor(String name) {
  10. this.name = name;
  11. }
  12. public abstract Destination getDestination();
  13. public Destination createDefaultDestination(int type){
  14. Destination dest=null;
  15. switch(type){
  16. case DESTIONATION_TYPE_TOPIC:
  17. dest=new Topic(){
  18. @Override
  19. public String getTopicName() throws JMSException {
  20. return defaultTopicName;
  21. }};break;
  22. case DESTIONATION_TYPE_QUEUE:
  23. dest=new Queue(){
  24. @Override
  25. public String getQueueName() throws JMSException {
  26. return defaultQueueName;
  27. }};
  28. break;
  29. }
  30. return dest;
  31. }
  32. }
package jms;
import javax.jms.*;
public abstract class JMSMessageActor {
	protected String name=null;
	protected String defaultQueueName="defalut-queue";
	protected String defaultTopicName="defalut-topic";
	protected final int  DESTIONATION_TYPE_TOPIC=1;
	protected final int DESTIONATION_TYPE_QUEUE=2;
	public JMSMessageActor(String name) {
		this.name = name;
	}
	public abstract Destination  getDestination();
	public Destination createDefaultDestination(int type){
		Destination dest=null;
		switch(type){
			case DESTIONATION_TYPE_TOPIC:
				dest=new Topic(){
					@Override
					public String getTopicName() throws JMSException {
						return defaultTopicName;
					}};break;
			case DESTIONATION_TYPE_QUEUE:
				dest=new Queue(){
					@Override
					public String getQueueName() throws JMSException {
						return defaultQueueName;
					}};
				break;
		}
		return dest;
	}
}

jms.JMSMessageConsumer
Java代码 复制代码 收藏代码
  1. package jms;
  2. import javax.jms.*;
  3. public abstract class JMSMessageConsumer extends JMSMessageActor implements Runnable,MessageListener{
  4. public JMSMessageConsumer(String name){
  5. super(name);
  6. }
  7. @Override
  8. public void onMessage(Message message) {
  9. synchronized(JMSMessageConsumer.class){
  10. System.out.println("##### consumer "+ name +" receive message. #####");
  11. System.out.println(JMSUtil.formatMessage(message));
  12. }
  13. }
  14. @Override
  15. public void run() {
  16. try{
  17. // get topic connect factory
  18. ConnectionFactory factory = JMSFactory.getActiveMQConnectionFactory();
  19. // create connection
  20. Connection connection = factory.createConnection();
  21. // create unique client id for the connection
  22. connection.setClientID("consumer_connection_"+name);
  23. // if the connection start method is not invoked , the consumer may be not receive the message
  24. connection.start();
  25. // create session
  26. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  27. Destination destination=getDestination();
  28. // if the destination is an instance of Queue ,
  29. // it will receive the message from the queue,
  30. // in other words the message can be consumed one time by one consumer
  31. // and the message is durable.
  32. // if the destination is an instance of Topic ,
  33. // the subscribers of the Topic can receive the message,
  34. // but the message is non durable.
  35. MessageConsumer consumer =session.createConsumer(destination,null,true);
  36. // if the destination is an instance of Topic,
  37. // specify the clientID of the connection
  38. // and create MessageConsumer like this,
  39. // the subscribers of the Topic can receive the message
  40. // and the message is durable.
  41. //consumer =session.createDurableSubscriber((Topic)destination, "durable topic", null, true);
  42. consumer.setMessageListener(this);
  43. }catch(Exception e){
  44. throw new RuntimeException(e);
  45. }
  46. }
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值