我编写了一个示例代码,将元素添加到activemq中,然后检索它们。我成功地添加了大约1000个元素,但在检索元素时,即使在队列中有很多元素时,代码也会在检索大约50-200个元素后卡住。即使队列不为空时,activemq消费者也不会返回数据
以下是我用于将元素添加到队列
@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();
}
的代码,这是我使用了从队列中检索元素
@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,其片段为
<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的配置文件。
我曾尝试过大量的研究,但我无法确定这个问题的根本原因。这将是非常有益的,如果任何人可以建议我我做错了什么