ActiveMQ学习笔记5-ActiveMQ简单使用

生产者

public static int MESSAGE_NUM = 2;//发送消息数量
    
    public static void main(String[] args) {
        ConnectionFactory factory;//JMS连接创建工厂
        Connection connection = null;//连接
        Session session;//会话
        Destination destination;//JMS的消息目的地,即ActiveMQ
        MessageProducer producer;//消息发送者
        //工厂实例化-实例出一个ActiveMQ的工厂
        factory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
        try {
            connection = factory.createConnection();//创建连接
            connection.start();//启动
            //获取一个会话
            session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("linwu");//获取目标队列
            producer = session.createProducer(destination); //获取消息发送者
            // 设置不持久化,此处学习,实际根据项目决定  
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
            System.out.println("服务器发送消息:");
            sendMessage(session, producer); //构造消息,此处写死,项目就是参数,或者方法获取
            session.commit();  
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {  
            try {  if (null != connection)  connection.close(); } catch (Throwable ignore) {  }  
        }  
}

private static void sendMessage(Session session, MessageProducer producer) throws JMSException {
        for (int i = 1; i <= MESSAGE_NUM; i++) {  
            TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i);  
            // 发送消息到目的地方  
            System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);  
            producer.send(message);  
        }          
    }

消费者

ConnectionFactory factory;
        Connection connection=null;
        Session session;
        Destination destination;
        MessageConsumer consumer;
        
        factory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
        try {
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("linwu");
            consumer = session.createConsumer(destination);
            System.out.println("客户端开始监听消息:");
            while(true) {
                TextMessage message = (TextMessage)consumer.receive(5000);
                if(null!=message) {
                    System.out.println(message.getText());
                }
            }
        } catch (JMSException e) {  e.printStackTrace();
        }finally {
            if(connection!=null) {
                try { connection.close();} catch (JMSException e) {e.printStackTrace();
                }
            }
        }

方法说明

ActiveMQConnectionFactory

第一个参数是用户名,第二个参数是密码。
第三个参数是activeMQ服务器的地址。Tcp开头是默认使用openwire协议,还有其他协议。

createSession参数

createSession(paramA,paramB);
paramA 取值有 : true or false 表示是否支持事务
paramB 取值有:Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE,SESSION_TRANSACTED
createSession(paramA,paramB);
paramA是设置事务的,paramB设置acknowledgment mode
1) paramA设置为false时:paramB的值可为Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE其中一个。
2) paramA设置为true时:paramB的值忽略, acknowledgment mode被jms服务器设置为SESSION_TRANSACTED 。
确认模式有三种:
1) Session.AUTO_ACKNOWLEDGE为自动确认,客户端发送和接收消息不需要做额外的工作。
2) Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后,必须调用javax.jms.Message的acknowledge方法。jms服务器才会删除消息。
3) DUPS_OK_ACKNOWLEDGE允许副本的确认模式。一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认消息的接收;而且允许重复确认。在需要考虑资源使用时,这种模式非常有效。

消费者同步和异步接收

同步:
上面的实例中,使用的是同步接收,调用receive方法。会一直阻塞到有消息到来。
异步:
注册使用MessageConsumer.setMessageListener() 注册一个 MessageListener 实现异步接收。代码如下:

public static MessageListener getMessageListener(){
        return new MessageListener() {
            public void onMessage(Message message) {
                if(null!=message) {
                    try {System.out.println(((TextMessage)message).getText());
                    } catch (JMSException e) { e.printStackTrace();}
                }
            }
        };}
//在应用是使用while
while(true) consumer.setMessageListener(getMessageListener());

持久化参数

producer.setDeliveryMode(int parm);
如果是DeliveryMode.NON_PERSISTENT表示不持久化,broker重新启动,数据将清空。
DeliveryMode.PERSISTENT表示持久化,数据会放入日志、数据库等MQ的持久化库。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值