以编程方式配置Apache ActiveMQ
我使用Apache ActiveMQ对大量消息进行排队,然后在一天结束时将它们出列。不过,我对ActiveMQ的运作方式感到困惑。在我的PC上,我没有安装ActiveMQ作为服务,也没有在某处安装服务器。我刚才包括“ActiveMQ的 - 全5.14.5.jar”作为我的项目Maven的依赖,我使用下面的代码至今:以编程方式配置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作为服务运行时使用的文件。我目前雇用只是罐库。
最初,我已经安装了Glassgfish服务器,以便使用JMS来排队所有消息,但自那之后我放弃了该项目,但IP仍在使用ActiveMQ(localhost:4848)。
请注意,以下是一个非常有效的示例-KahaDB也用于在发生服务器故障时存储消息。
就我而言,ActiveMQ确实从STS启动本地服务器,我正在运行此代码,但配置文件在哪里?我可以通过编程方式更改其属性?
这应该工作(适用于我们与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);
将使第一清洁运行输出“接收:重要任务”
删除'KahaDB'解决了这个问题。谢谢! – Lefteris008
你尝试像'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