繁体   English   中英

如何在ActiveMQ中配置Producer

[英]How to configure Producer in ActiveMQ

“ BrokerFactory”从xml文件获取配置。 如何配置生产者?

遵循以下示例:
http://activemq.apache.org/hello-world.html
我将以下内容用于生产者联系

ActiveMQConnectionFactory connectionFactory = ...

我找不到在何处更改其端口。在“ Hello-World”很好用之前,直到我想将经纪人生产者和消费者划分为不同的流程。

(我正在使用Java SE,在Mint 17上运行)

我发布了我正在使用的Java代码,希望对您有所帮助。 生产者和消费者都在自己的Runnable类中。

生产者方代码:

public void run(){
        logger.debug("JMS Sender get started to send responses to activeMq...");

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);

        connectionFactory.setUseAsyncSend(true);//faster than sync
        connectionFactory.setOptimizeAcknowledge(true);
        Connection connection;

        try{
            connection = connectionFactory.createConnection();
            connection.start();

            /** create an activeMq for responses **/
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue(jmsResponseQueueName);
            MessageProducer responseProducer = session.createProducer(destination);
            responseProducer.setDeliveryMode(DeliveryMode.PERSISTENT); //persistent mode

            while (keepRunning){
             // do time-consuming work here
            }
        }
        catch (JMSException e){
            logger.error("Exception occur when sender fails to connect to activeMQ:{}", e.getMessage());
        }
        catch (InterruptedException ie){
            logger.error("Exception occur when take response from queue...{}", ie.getMessage());
        }
    }

消费者方:

@Override public void run(){
     ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(jmsUser, jmsPassword, jmsUrl);

    try {
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(jmsQueueName);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(this);
        connection.start();
    }
    catch (JMSException jmse){
        logger.error("JMSException occur: {}", jmse.getMessage());
    }
}

/**
 * Receiving incoming responses from activemq, and then store the received response
 * into a linked blocking response queue.
 * @param message 
 */
@Override public void onMessage(Message message){
    try {
        if(message != null){
            // consume the valid message 
        }
    }
    catch (JMSException  exception){
        logger.error("Exception is thrown when handle received message: {}", exception.getMessage());
    }
    catch (InterruptedException exception){
        logger.error("Exception is thrown when handle received message: {}", exception.getMessage());
    }
}

URL像这样,其中包含port

"failover://(tcp://localhost:61616)?maxReconnectAttempts=0"

usernamepassword默认为admin

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM