2016-12-27 5 views
0

のexcecutedが実行されていません。この例では、デバッグポイントはconnection.start(); パブリッククラスRerateProducer {connection.start();この例では、JMSアプリケーションで

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; 
} 
+0

try/catchブロック全体にコードを入れ、スローされた例外をキャプチャして出力します。コードがconnection.start()メソッドにヒットする前に例外がスローされていると思います。 – Shashi

答えて

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> 

これが役立ちます。幸せなコーディング!

関連する問題