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からローカルサーバーを起動しますか?プロパティをプログラムで変更することはできますか?
あなたが '' broker.setSchedulerSupport(真)のようなものを試してみましたか? (http://activemq.apache.org/maven/5.11.0/apidocs/org/apache/activemq/broker/BrokerService.html#setSchedulerSupport(boolean)を参照してください) – Tome
私はやった、それは動作しませんでした。 – Lefteris008
あなたは '_AMQ_SCHED_DELIVERY'プロパティを使用していますか? ActiveMQのプロパティはむしろ 'AMQ_SCHEDULED_DELAY'のようになります(http://activemq.apache.org/delay-and-schedule-message-delivery.htmlを参照) – Tome