activemq编程式客户端-创新互联

dependency

创新互联建站专注于琼山企业网站建设,响应式网站,购物商城网站建设。琼山网站建设公司,为琼山等地区提供建站服务。全流程按需设计,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务

    org.apache.activemq
    activemq-client
    ${activemq.version}


    org.apache.activemq
    activemq-spring
    ${activemq.version}

ActivemqProducerTest

public class ActivemqProducerTest {

    private static final String producer_broker_url = "failover:(tcp://localhost:61617,tcp://localhost:61618,tcp://localhost:61619)";
    private static final String username = "admin";
    private static final String password = "admin123";
    private static final String destination_queue = "queue.test01";
    private static final String destination_topic = "topic.test01";

    public static void main(String[] args) throws JMSException {
        produceInQueue();
        produceInTopic();
    }

    private static void produceInQueue() throws JMSException {
        // activeMQ connection factory
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(producer_broker_url);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQQueue dest = new ActiveMQQueue(destination_queue);

        TextMessage textMessage = session.createTextMessage();
        textMessage.setText("this is a test...");

        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        producer.send(textMessage);

        connection.close();
    }

    private static void produceInTopic() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(producer_broker_url);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQTopic dest = new ActiveMQTopic(destination_topic);

        MapMessage mapMessage = session.createMapMessage();
        mapMessage.setString("key01","val01");
        mapMessage.setInt("key02", 2);

        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        producer.send(mapMessage);

        connection.close();
    }
}
ActivemqCustomerTest
public class ActivemqCustomerTest {

    private static final String producer_broker_url = "failover:(tcp://localhost:61617,tcp://localhost:61618,tcp://localhost:61619)";
    private static final String username = "admin";
    private static final String password = "admin123";
    private static final String destination_queue = "queue.test01";
    private static final String destination_topic = "topic.test01";

    public static void main(String[] args) throws JMSException {
        consumeTopic();
        consumeTopic();
        consumeQueue();
    }

    private static void consumeTopic() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(producer_broker_url);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setExceptionListener(new ExceptionListener(){
            @Override
            public void onException(JMSException exception) {
                System.out.println("JMSException:"+exception.getMessage());
            }
        });
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQTopic dest = new ActiveMQTopic(destination_topic);

        MessageConsumer consumer = session.createConsumer(dest);
        consumer.setMessageListener(new MessageListener(){
            @Override
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage){
                        System.out.println("message = [" + ((TextMessage) message).getText() + "]");
                    }
                    if (message instanceof MapMessage){
                        MapMessage mapMessage = (MapMessage) message;
                        System.out.println("key01 = [" + mapMessage.getString("key01") + "]");
                        System.out.println("key02 = [" + mapMessage.getInt("key02") + "]");
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        //   connection.close();
    }


    private static void consumeQueue() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(producer_broker_url);
        activeMQConnectionFactory.setUserName(username);
        activeMQConnectionFactory.setPassword(password);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setExceptionListener(new ExceptionListener(){
            @Override
            public void onException(JMSException exception) {
                System.out.println("JMSException:"+exception.getMessage());
            }
        });
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQQueue dest = new ActiveMQQueue(destination_queue);

        MessageConsumer consumer = session.createConsumer(dest);
        consumer.setMessageListener(new MessageListener(){
            @Override
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage){
                        System.out.println("message = [" + ((TextMessage) message).getText() + "]");
                    }
                    if (message instanceof MapMessage){
                        MapMessage mapMessage = (MapMessage) message;
                        System.out.println("key01 = [" + mapMessage.getString("key01") + "]");
                        System.out.println("key02 = [" + mapMessage.getInt("key02") + "]");
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        //   connection.close();
    }
}

集群配置地址:https://blog.51cto.com/881206524/2129863

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:activemq编程式客户端-创新互联
网站网址:http://scjbc.cn/article/dhsgej.html

其他资讯