工具使用篇——java操作activemq

准备

不管是本地还是云端,要先有一个activemq

安装过程

生产者
Publiser.java

package example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTempQueue;
import org.apache.activemq.command.ActiveMQTopic;

import javax.jms.*;
class Publisher {
    public static void main(String []args) throws JMSException {
        String user = "admin";
        String password = "admin";
        String host = "127.0.0.1";
        int port = Integer.parseInt("61616");
        String destination = "flight_tag";
        int messages = 100;

        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
        Connection connection = factory.createConnection(user, password);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Destination dest = new ActiveMQTopic(destination);
        Destination dest= new ActiveMQQueue(destination);
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for( int i=1; i <= messages; i ++) {
            TextMessage msg = session.createTextMessage(String.valueOf(i));
            msg.setIntProperty("id", i);
            producer.send(msg);
            if( (i % 100) == 0) {
                System.out.println(String.format("Sent %d messages", i));
            }
        }

//        producer.send(session.createTextMessage("SHUTDOWN"));
        connection.close();

    }

    private static String env(String key, String defaultValue) {
        String rc = System.getenv(key);
        if( rc== null )
            return defaultValue;
        return rc;
    }

    private static String arg(String []args, int index, String defaultValue) {
        if( index < args.length )
            return args[index];
        else
            return defaultValue;
    }

}

消费者
listener.java

package example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import javax.jms.*;

class Listener {
    public static void main(String []args) throws JMSException {
        String user = "admin";
        String password = "admin";
        String host = "127.0.0.1";
        int port = Integer.parseInt("61616");
        String destination = "flight_tag";
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
        Connection connection = factory.createConnection(user, password);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Destination dest = new ActiveMQTopic(destination);
        Destination dest = new ActiveMQQueue(destination);
        MessageConsumer consumer = session.createConsumer(dest);
        long start = System.currentTimeMillis();
        long count = 1;
        System.out.println("Waiting for messages...");
        while(true) {
            Message msg = consumer.receive();
            if( msg instanceof  TextMessage ) {
                String body = ((TextMessage) msg).getText();
//               在此执行方法
                System.out.println(body);
                if( "SHUTDOWN".equals(body)) {
                    long diff = System.currentTimeMillis() - start;
//                    System.out.println(String.format("Received %d in %.2f seconds", count, (1.0*diff/1000.0)));
                    break;
                } else {
                    System.out.println("继续监听");
                }

            } else {
                System.out.println("Unexpected message type: "+msg.getClass());
            }
        }
        connection.close();
    }



    private static String arg(String []args, int index, String defaultValue) {
        if( index < args.length )
            return args[index];
        else
            return defaultValue;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值