ActiveMQ怎么实现多个生产者向一个队列目标发送信息?求教,最好有代码demo

ActiveMQ怎么实现多个生产者向一个队列目标发送信息?求教,最好有代码demo

1个回答

package org.ygy.mq.lesson02;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class SimpleQueueProducer {

    public void produce() throws Exception {
        ConnectionFactory factory = new ActiveMQConnectionFactory();
        Connection connection = factory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("queue_lesson");


        MessageProducer producer = session.createProducer(destination);
        for(int i=0; i<3; i++ ) {
            TextMessage message = session.createTextMessage(new Date() + " ");
            producer.send(message);

            Thread.sleep(1000);
        }


        connection.close();
    }

}


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐