ActiveMQ使用笔记-1

18 篇文章 0 订阅
1 篇文章 0 订阅

JMS(Java Message Service)即Java消息服务。它提供标准的产生、发送、接收消息的接口简化应用的开发。它支持两种消息通信模型:

1.点到点(point-to-point)模型:P2P模型规定了一个消息只能有一个接收者,消息生产者产生一个消息后,把这个消息发送到一个QUEUE队列中,然后消息接收者在从这个队列中读取这个消息,一旦这个消息被一个接收者读取之后,它就在这个QUEUE中消失了,所以一个消息只能被一个消息接收者消费。

2.发布/订阅(Pub/Sub)模型:P/S模型允许一个消息可以有多个接收者,消息生产者产生一个消息后,就把这个消息发送到一个Topic中,这个Topic可以同时有多个接收者在监听,当一个消息到达这个Topic之后,所有消息接收者都会接收这个消息。


利用ActiveMQ进行编程,一般的步骤如下:


消息产生者向JMS发送消息的步骤:

1.生产者创建一个连接,使用工厂类JMS ConnectionFactory

2.使用管理对象JMS ConnectionFactory建立连接Connection 

3.使用连接Connection 建立会话Session 

4.使用会话Session和管理对象Destination创建消息生产者MessageSender 

5.使用消息生产者MessageSender发送消息


消息消费者从JMS接受消息的步骤:

1.创建连接使用的工厂类JMS ConnectionFactory 

2.使用管理对象JMS ConnectionFactory建立连接Connection 

3.使用连接Connection 建立会话Session 

4.使用会话Session和管理对象Destination创建消息消费者MessageReceiver 

5.使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver 消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。


下面安装这个步骤简单的写一个例子,在验证这个例子的时候,必须确保已经下载并启动了ActiveMQ服务器。

```

public abstract class ActiveMQBase {
    // ConnectionFactory:连接工厂,JMS用它创建连接
    protected static ConnectionFactory connectionFactory;
    // ConnectionJMS客户端到JMS Provider的连接
    protected static Connection connection = null;
    // Session:一个发送或接收消息的线程
    protected static Session session;
    // Destination:消息的目的地;消息发送给谁.
    protected static Destination destination;

    protected static void init() {

        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "failover:(tcp://127.0.0.1:61616)");
        
        try {

          connection = connectionFactory.createConnection();
            //启动
            connection.start();
         
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
           
            destination = session.createQueue("QueueName");
        } catch (JMSException e) {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {

            }
        }
    }
}


public class ActiveMQReceive extends ActiveMQBase {
    private static MessageConsumer consumer;

    public static void main(String[] args) {
        init();
        try {
            consumer = session.createConsumer(destination);
            while (true) {
              
                TextMessage message = (TextMessage) consumer.receive(100000);
                if (null != message) {
                    System.out.println("收到消息" + message.getText());
                } else {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {

            }
        }
    }
}


public class ActiveMQSender extends ActiveMQBase {

    private static final int SEND_NUMBER = 10;
    private static MessageProducer producer;

    public static void sendMessage(final Session session, final MessageProducer producer) throws JMSException {

        Runnable runnable = new Runnable() {
            public void run() {
                try {
                    TextMessage message = session.createTextMessage("ActiveMq发送的消息" + System.currentTimeMillis());
                    producer.send(message);
                    session.commit();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        };

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);

    }

    public static void main(String[] args) {
        init();
        try {
            //得到消息生成者【发送者】
            producer = session.createProducer(destination);
            //设置不持久化,此处学习,实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        
            sendMessage(session, producer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值