2017-07-03 10 views
0

Apache ActiveMQを使用して大量のメッセージをキューに入れ、終わりにデキューします。私はActiveMQの機能について混乱しています。私のPCには、ActiveMQをサービスとしてインストールしていないし、どこかにサーバーのインストールがない。私はちょうど私のプロジェクトでMavenの依存関係として「ActiveMQの-すべて-5.14.5.jarを」が含まれていると私は、これまでに以下のコードを使用しています:あなたが見ることができるようにプログラムでApache ActiveMQを設定する

public static void main(String[] args) throws URISyntaxException, Exception { 
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:4848)")); 
    broker.start(); 
    Connection connection = null; 
    try { 
     // Producer 
     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:4848"); 
     connection = connectionFactory.createConnection(); 
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     Queue queue = session.createQueue("customerQueue"); 
     String payload = "Important Task"; 
     Message msg = session.createTextMessage(payload); 
     MessageProducer producer = session.createProducer(queue); 
     System.out.println("Sending text '" + payload + "'"); 
     msg.setLongProperty("_AMQ_SCHED_DELIVERY", System.currentTimeMillis() + 5000L); 
     producer.send(msg); 

     // Consumer 
     MessageConsumer consumer = session.createConsumer(queue); 
     connection.start(); 
     QueueBrowser browser = session.createBrowser(queue); 
     while (browser.getEnumeration().hasMoreElements()) { 
      TextMessage textMsg = (TextMessage) consumer.receive(); 
      browser.getEnumeration().nextElement(); 
      System.out.println(textMsg); 
      System.out.println("Received: " + textMsg.getText()); 
     } 

     session.close(); 
    } finally { 
     if (connection != null) { 
      connection.close(); 
     } 
     broker.stop(); 
    } 
} 

が、私は遅らせたいですメッセージは5秒(またはそれ以上、変化する可能性があります)ですが、私が見つけたすべてのガイドでは、XML設定ファイルを設定するように指示されています。しかし、これはActiveMQをサービスとして実行するときに使用されるファイルです。私は現在、のjarライブラリを採用しています。

最初に、JMSを使用してすべてのメッセージをキューイングするためにGlassgfishサーバーをインストールしましたが、その後はプロジェクトを中止しましたが、IPはまだActiveMQ(localhost:4848)から使用されています。

以下は完全な使用例です.KahaDBは、サーバーに障害が発生した場合にもメッセージを格納するために使用されています。

ActiveMQは私がこのコードを実行しているが、構成ファイルはどこにあるのかSTSからローカルサーバーを起動しますか?プロパティをプログラムで変更することはできますか

+0

あなたが '' broker.setSchedulerSupport(真)のようなものを試してみましたか? (http://activemq.apache.org/maven/5.11.0/apidocs/org/apache/activemq/broker/BrokerService.html#setSchedulerSupport(boolean)を参照してください) – Tome

+0

私はやった、それは動作しませんでした。 – Lefteris008

+0

あなたは '_AMQ_SCHED_DELIVERY'プロパティを使用していますか? ActiveMQのプロパティはむしろ 'AMQ_SCHEDULED_DELAY'のようになります(http://activemq.apache.org/delay-and-schedule-message-delivery.htmlを参照) – Tome

答えて

2

これは機能するはずです(ActiveMQ 5.12.3で動作します)。以前のメッセージを読むのを避けるために、まずKahaDBストアを清掃してください。

public static void main(String[] args) throws Exception { 
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:4848)")); 
    broker.setSchedulerSupport(true); 
    broker.start(); 
    Connection connection = null; 
    try { 
     // Producer 
     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:4848"); 
     connection = connectionFactory.createConnection(); 
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     Queue queue = session.createQueue("customerQueue"); 
     String payload = "Important Task"; 
     Message msg = session.createTextMessage(payload); 
     MessageProducer producer = session.createProducer(queue); 
     System.out.println("Sending text '" + payload + "'"); 
     msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5000L); 
     producer.send(msg); 
     connection.start(); 

     // Consumer 
     MessageConsumer consumer = null; 
     consumer = session.createConsumer(queue); 

     QueueBrowser browser = session.createBrowser(queue); 
     while (browser.getEnumeration().hasMoreElements()) { 
      TextMessage textMsg = (TextMessage) consumer.receive(); 
      browser.getEnumeration().nextElement(); 
      System.out.println(textMsg); 
      System.out.println("Received: " + textMsg.getText()); 
     } 

     session.close(); 
    } finally{ 
     if (connection != null) { 
      connection.close(); 
     } 
     broker.stop(); 
    } 
} 

(空KahaDB店で)最初のクリーンランは

は出力 "受信:重要な仕事" ではないはずです

、第二の意志のに対し、あなたが削除しない場合その間のデータファイル。ライン `

msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY、5000L)を除去

最初のクリーンラン出力になるだろう「受信は:重要な仕事」

+0

'KahaDB'を削除することで問題は解決しました。ありがとう! – Lefteris008

関連する問題