connection.start();没有在JMS应用程序中获得批准

时间:2016-12-27 06:49:57

标签: java jms

在这个例子中,调试点没有返回到connection.start()行; 公共类RerateProducer {

private Session session;
private Connection connection;
private MessageProducer producer;
private boolean isActive;

public RerateProducer(String destination) throws JMSException, NotValidException {
    if (destination == null || destination.isEmpty()) {
        throw new NotValidException("Destination cannot be null or empty");
    }
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("failover:tcp://" + ReportServerConstants.ACTIVEMQ.ACTIVEMQ_HOST_RERATE + ":" + ReportServerConstants.ACTIVEMQ.ACTIVEMQ_PORT);
    connection = factory.createConnection(ReportServerConstants.ACTIVEMQ.ACTIVEMQ_USER, ReportServerConstants.ACTIVEMQ.ACTIVEMQ_PASSWORD);
    System.out.println("*************Before con startttttttt  connection from RerateProducer:"+connection);
    connection.start();
    System.out.println("connection from RerateProducer:"+connection);
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session.createQueue(destination);
    producer = session.createProducer(dest);
    producer.setTimeToLive(ReportServerConstants.TIME_CONSTANTS.TIME_TO_LIVE_FOR_MESSAGE);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    isActive = true;
}

1 个答案:

答案 0 :(得分:0)

嗯,你正在使用故障转移连接,字面上它应该有多个jms url以防故障转移。你能不能试试下面的语法。

failover:(Uri1,Uri2,Uri3,...,UriN)

在你的情况下它应该是这样的。

failover:(tcp://ReportServerConstants.ACTIVEMQ.ACTIVEMQ_HOST_RERATE + ":" + ReportServerConstants.ACTIVEMQ.ACTIVEMQ_PORT)

尝试使用此代码。

package com.demo;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class HelloWorldProducer implements Runnable {

    public void run() {
        try {
            // Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(tcp://localhost:61616)");

            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("TEST.FOO");

            // Create a MessageProducer from the Session to the Topic or Queue
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            // Create a messages
            String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
            TextMessage message = session.createTextMessage(text);

            // Tell the producer to send the message
            System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());
            producer.send(message);

            // Clean up
            session.close();
            connection.close();
        } catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }

    }

}


package com.demo;

public class JMSTest {

    public static void main(String[] args) {
        new Thread(new HelloWorldProducer()).start();
    }

}

使用apache-activemq-5.14.3版本对我来说非常适合。我也测试了它。您可能还需要将以下Maven依赖项添加到pom.xml文件中。

<dependencies>
    <!-- https://mvnrepository.com/artifact/javax.jms/javax.jms-api -->
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>javax.jms-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.7.0</version>
    </dependency>
</dependencies>

希望这会有所帮助。快乐的编码!