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

时间:2016-03-30 07:18:42

标签: java proxy activemq

目前,我在通过代理服务器向Internet发送ActiveMQ邮件时遇到问题。

我的网络架构:

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

我搜索了ActiveMQ的文档,但没有找到任何东西,ActiveMQ API也是如此。 http://activemq.apache.org/tcp-transport-reference.html

  

是否可以通过代理发送JMS消息?解决这个问题的任何方法?

我的代码在局域网上运行良好,但是当通过代理发送时,会引发错误:

代码:

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

2 个答案:

答案 0 :(得分:1)

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

尝试使用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发布/使用消息。

答案 1 :(得分:1)

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

}
相关问题