【发布时间】:2011-10-15 01:00:02
【问题描述】:
我希望 jms 在一个线程处理了一条消息时收到一条消息(threadPool 提交一个可调用的)。 消息由主线程接收。 下面哪种方式更好:
我使用的是 spring 3.0.5:
ApplicationContext context = new ClassPathXmlApplicationContext(
"application-context.xml");
jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
destination = (Destination) context.getBean("destination");
_log4j.debug("ThreadSize in xml\t"
+ appConfig.getThumbCreatorThreadSize());
在主线程方式一:
while (countFlag < 0) {
try {
TextMessage msg = (TextMessage) jmsTemplate
.receive(destination);
// prehandle ,then give to sub workers.
if (msg != null) {
_log4j.debug("JMSMessageID:\t" + msg.getJMSMessageID()
+ "\t" + msg.getText());
IConsumer thumbConsumerImpl = null;
thumbConsumerImpl = new ThumbConsumerTaskImpl(msg);
Future<List<ThumbCreatorInfo>> result = threadPool
.submit((Callable<List<ThumbCreatorInfo>>) thumbConsumerImpl);
}
} catch (IllegalArgumentException e) {
_log4j.warn(e.getMessage(), e);
} catch (JMSException e) {
_log4j.error("Please check the queue server!JMSException!", e);
} catch (Exception e) {
_log4j.error("", e);
}
}
在主线程方式2:
TextMessage msg = (TextMessage) jmsTemplate.receive(destination);
do {
try {
// prehandle ,then give to sub workers.
if (msg != null) {
_log4j.debug("JMSMessageID:\t" + msg.getJMSMessageID()
+ "\t" + msg.getText());
IConsumer thumbConsumerImpl = null;
thumbConsumerImpl = new ThumbConsumerTaskImpl(msg);
Future<List<ThumbCreatorInfo>> result = threadPool
.submit((Callable<List<ThumbCreatorInfo>>) thumbConsumerImpl);
}
msg = (TextMessage) jmsTemplate.receive(destination);
} catch (IllegalArgumentException e) {
_log4j.warn(e.getMessage(), e);
} catch (JMSException e) {
_log4j.error("Please check the queue server!JMSException!", e);
} catch (Exception e) {
_log4j.error("", e);
}
} while (countFlag < 0);
【问题讨论】:
标签: java spring jms threadpool activemq