简体   繁体   中英

How to send message from ActiveMQ over proxy

Currently, I have a trouble with sending ActiveMQ message to Internet over proxy server.

My network architecture:

JMS Sender ---- |Proxy| --- JMS server (xx.xx.xx.xx) [on Internet]

I searched on ActiveMQ's documentation but found nothing, ActiveMQ API too. http://activemq.apache.org/tcp-transport-reference.html

Is it possible to send JMS message over proxy? Any solution for this problem?

My code work well on LAN, but when send over proxy, it raise error:

Code:

public void createConnection() throws JMSException {
   String jmsURL = "tcp://xx.xx.xx.xx:61616";
   TopicConnectionFactory factory
           = (TopicConnectionFactory) new ActiveMQConnectionFactory(jmsURL);
   TopicConnection connection = factory.createTopicConnection(); //Error here
   TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   Topic topic = session.createTopic(topicName);
   TopicPublisher publisher = session.createPublisher(topic);
   publisher.setPriority(PRIORITY);
   publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}

Error:

Exception in thread "main" javax.jms.JMSException: 
Could not connect to broker URL: tcp://xx.xx.xx.xx:61616. Reason: java.net.ConnectException: Connection timed out: connect
    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.createTopicConnection(ActiveMQConnectionFactory.java:279)
    at JMSSender.createConnection(JMSSender.java:55)
    at MainClass.main(MainClass.java:142)
Caused by: java.net.ConnectException: Connection timed out: connect

The problem will probably be on the proxy itself. If your proxy does not allow your protocol and/or your destination, it will block all your requests.

Try to use HTTP (or HTTPS) protocol instead of TCP, because proxies usually allow this kind of requests.

So, add an HTTP transport connector to your broker and try again using HTTP from your client:

<transportConnectors>
   <transportConnector name="tcp" uri="tcp://xx.xx.xx.xx:61616?trace=true"/>
   <transportConnector name="http" uri="http://xx.xx.xx.xx:8080?trace=true" />
</transportConnectors>

Take a look at: HTTP and HTTPS transports

On the other hand, you can also try the REST API to publish/consume messages.

**Use this code , it will work** 

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

import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {

    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null;
    private Destination destination = null;
    private MessageProducer producer = null;

    public Sender() {

    }

    public void sendMessage() {

        try {
//          factory = new ActiveMQConnectionFactory(
//                  ActiveMQConnection.DEFAULT_BROKER_URL);

            factory = new ActiveMQConnectionFactory(
                    "admin",
                    "admin", "nio://10.10.10.10:61616");
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("sample.queue");
            producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage();
            message.setText("Hello ...This is a sample message.. "+i);
            producer.send(message);
            System.out.println("Sent: " + message.getText());
            connection.stop();
            session.close();
            connection.close();

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Sender sender = new Sender();
        sender.sendMessage();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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