javahttpactivemq-classichttpconnection

ActiveMQ http connection refused


I am trying to connect my java client application to ActiveMQ over http, but keep getting an error:

Failed to perform GET on: http://localhost:8080 as response was: Connection refused: connect

This connections works why I connect with: tcp://localhost:61616

activemq.xml

<broker>
  ...
  <transportConnectors>
    <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="http" uri="http://0.0.0.0:8080"/>
  </transportConnectors>
  ...
</broker>

jndi.properties in my Java Client program:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
#java.naming.provider.url = tcp://localhost:61616
java.naming.provider.url = http://localhost:8080
connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry
queue.testQueue = testQueue

Java Client

package be.jmshub.jmsremotejndiproducer;

import javax.jms.JMSException;
import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Producer {

    public static void main(String[] args) throws JMSException {

        System.out.println("JMS REMOTE PRODUCER: start");

        try {
            // create a new intial context, which loads from jndi.properties file
            Context ctx = new InitialContext();
            QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("connectionFactory");
            Connection connection = factory.createConnection();
            Queue queue = (javax.jms.Queue) ctx.lookup("testQueue");
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            MessageProducer producer = session.createProducer(queue);

            System.out.println("JMS REMOTE PRODUCER: connected");

            int count = 0;

            for (int i = 0; i < 5; i++) {
                String messageText = "This is message " + (i + 1);
                Message message = session.createTextMessage(messageText);
                System.out.println("JMS REMOTE PRODUCER: sending - " + messageText);
                producer.send(message);
                count += 1;
            }
            System.out.println("JMS REMOTE PRODUCER: total messages sent: " + count);

            // Clean up
            session.close();
            connection.close();
        } catch (Exception ex) {
            System.out.println("JMS REMOTE PRODUCER: EXCEPTION:" + ex.toString());
        }
        System.out.println("JMS REMOTE PRODUCER: finished");
    }
}

This works with tcp://localhost:61616 but not with http://localhost:8080

Can any person help me get this thing up and running?


Solution

  • Find below a stripped down example to start the broker. Your Producer runs without any problem.

    activemq.xml

    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:amq="http://activemq.apache.org/schema/core"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
    
        <broker xmlns="http://activemq.apache.org/schema/core" start="false">
            <transportConnectors>
                <transportConnector name="openwire" uri="tcp://0.0.0.0:0?maximumConnections=1000"/>
                <transportConnector name="http" uri="http://0.0.0.0:8080"/>
            </transportConnectors>
        </broker>
    </beans>
    

    Broker.java

    package be.jmshub.jmsremotejndiproducer;
    
    import org.apache.activemq.broker.BrokerFactory;
    import org.apache.activemq.broker.BrokerService;
    
    public class Broker {
    
        public static void main(String[] args) throws Exception {
            BrokerService broker = BrokerFactory.createBroker("xbean:foo.xml");
            broker.setPersistent(false);
            broker.start();
            broker.waitUntilStarted();
        }
    }