ozma
ozma

Reputation: 1803

How to configure Producer in ActiveMQ

The "BrokerFactory" takes configuration from an xml file. How can I configure the producer?

Following this example:
http://activemq.apache.org/hello-world.html
Iv'e used the following for the producer connections

ActiveMQConnectionFactory connectionFactory = ...

I couldn't find where to change its ports Following the "Hello-World" was fine until I've wanted to separate broker producer and consumer to different processes.

(I am using Java SE, running on mint 17)

Upvotes: 0

Views: 147

Answers (1)

Haifeng Zhang
Haifeng Zhang

Reputation: 31895

I post Java code that I am using, hope it helps. Producer and Consumer are in their own Runnable classes.

Producer side code:

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());
        }
    }

Consumer side:

@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());
    }
}

The URL is like this and it contains the port:

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

username and password are admin by default.

Upvotes: 1

Related Questions