如何通过代理从 ActiveMQ 发送消息

How to send message from ActiveMQ over proxy

目前,我无法通过代理服务器向 Internet 发送 ActiveMQ 消息。

我的网络架构:

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

我搜索了 ActiveMQ 的文档但一无所获,ActiveMQ API 也是如此。 http://activemq.apache.org/tcp-transport-reference.html

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

我的代码在 LAN 上运行良好,但通过代理发送时,会引发错误:

代码:

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

错误:

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

问题可能出在代理本身上。如果您的代理不允许您的协议 and/or 您的目的地,它将阻止您的所有请求。

尝试使用 HTTP(或 HTTPS)协议而不是 TCP,因为代理通常允许这种请求。

因此,向您的代理添加一个 HTTP 传输连接器,然后从您的客户端使用 HTTP 重试:

<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>

看看:HTTP and HTTPS transports

另一方面,您也可以尝试 REST API 到 publish/consume 条消息。

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

}