2 years ago

#76322

test-img

siberloewe

ActiveMQ in Eclipse to listen to remote broker

My goal is to listen to a broker on a port on a specific IP address with a given topic to receive JMS messages from there with ActiveMQ in Eclipse IDE.

Here is my code:

public static void main(String[] args) throws URISyntaxException, Exception {
    
    Connection Connection = null;
    try {
        // Producer
        ConnectionFactory ConnectionFactory = new ActiveMQConnectionFactory("tcp://1.2.3.4:5678");
        
        Connection = ConnectionFactory.createConnection();
        Connection.start();
        Session session = Connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
        Topic topic = session.createTopic("MyTopic");
        
        // Consumer1 subscribes to customerTopic
        MessageConsumer consumer1 = session.createConsumer(topic);
        consumer1.setMessageListener(new MessageConsumeListener("MyTopic"));
               
        System.out.print("connected");
        Thread.sleep(3000);
        /* session.close(); */
    } finally {
        if (Connection != null) {
            Connection.close();
        }
    }
}

This is the error:

Exception in thread "main" javax.jms.JMSException: Could not connect to broker URL: tcp://1.2.3.4.5678 Reason: java.net.ConnectException: Connection refused (Connection refused)
    at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:36)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:360)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:305)
    at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:245)
    at My.Project.subscribelistener.main(subscribelistener.java:29)

And also this:

Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at org.apache.activemq.transport.tcp.TcpTransport.connect(TcpTransport.java:501)
    at org.apache.activemq.transport.tcp.TcpTransport.doStart(TcpTransport.java:464)
    at org.apache.activemq.util.ServiceSupport.start(ServiceSupport.java:55)
    at org.apache.activemq.transport.AbstractInactivityMonitor.start(AbstractInactivityMonitor.java:168)
    at org.apache.activemq.transport.InactivityMonitor.start(InactivityMonitor.java:50)
    at org.apache.activemq.transport.TransportFilter.start(TransportFilter.java:58)
    at org.apache.activemq.transport.WireFormatNegotiator.start(WireFormatNegotiator.java:72)
    at org.apache.activemq.transport.TransportFilter.start(TransportFilter.java:58)
    at org.apache.activemq.transport.TransportFilter.start(TransportFilter.java:58)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:340)
    ... 3 more

I can also ping the IP address and with telnet I can connect to the port.

How can I connect to a remote broker?

eclipse

maven

activemq

0 Answers

Your Answer

Accepted video resources