【发布时间】:2016-03-01 11:13:21
【问题描述】:
我编写了一个示例代码来将元素添加到 activemq,然后检索它们。我成功地添加了大约 1000 个元素,但是在检索元素时,即使队列有很多元素,在检索到大约 50 到 200 个元素后,代码也会以某种方式卡住。
以下是我用于向队列添加元素的代码
@POST
@Path("/addelementtoqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addElementToQeueue(@FormParam("count") int count) throws Exception {
IntStream.range(0, count)
.forEach(e -> {
try {
addElement(e);
}catch(Exception e1) {
throw new RuntimeException(e1);
}
});
}
private void addElement(int i) throws Exception {
Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
conn.start();
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer prod = session.createProducer(queue);
prod.send(queue, session.createTextMessage("message "+ i), DeliveryMode.PERSISTENT, 4, 0);
prod.close();
session.close();
conn.close();
}
这是我用来从队列中检索元素的 sn-p
@POST
@Path("/removeelementfromqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void removeElementToQeueue(@FormParam("count") int count) throws Exception {
IntStream.range(0, count)
.forEach(e -> {
try {
extractElement();
}catch(Exception e1) {
throw new RuntimeException(e1);
}
});
}
private void extractElement() throws Exception {
Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
conn.start();
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
queue = session.createQueue("walkin.testing");
MessageConsumer consumer = session.createConsumer(queue);
TextMessage msg = (TextMessage)consumer.receive();
System.out.println(msg.getText());
msg.acknowledge();
consumer.close();
session.close();
conn.close();
}
我通过resource.xml获取连接工厂,同样的sn-p是
<resources>
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
BrokerXmlConfig = jdbcBroker:(tcp://0.0.0.0:61616)
ServerUrl = tcp://0.0.0.0:61616?jms.prefetchPolicy.queuePrefetch=0
</Resource>
<Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
ResourceAdapter = MyJmsResourceAdapter
</Resource></resources>
我正在使用 activeMQ 5.13.1,带有 apache-tomee-plus-1.7.2 和 Java 8,jdbc 存储为 mysql。我已经配置了activemq-jdbc-performance.xml作为apache activemq的配置文件。
我已尝试对此进行大量研究,但我无法确定此问题的根本原因。如果有人可以建议我做错了什么,这将非常有帮助
【问题讨论】:
标签: jms activemq java-ee-6 producer-consumer