JMS Api Demo

来源:互联网 发布:简述osi七层网络模型 编辑:程序博客网 时间:2024/06/06 03:09
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. }

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. }

jms.JMSMessageConsumer
Java代码 复制代码 收藏代码
  1. package jms;
  2. import javax.jms.*;
  3. public abstract class JMSMessageConsumer extends JMSMessageActorimplements 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. }  
原创粉丝点击