JMS和ThreadPool有问题吗? [英] JMS and ThreadPool problem?

查看:117
本文介绍了JMS和ThreadPool有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当一个线程处理了一条消息(threadPool提交可调用对象)时,jms会收到一条消息. 消息是由主线程接收的. 下面哪种方法更好:

I want that jms receives a message when one thread has handled a message (threadPool submits a callable). The messages are received by a master thread. Which way is better below:

我使用spring 3.0.5:

I use 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());

在主线程中的方式1:

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);

推荐答案

我不确定我知道您要做什么.如果您试图同时处理多个消息,请远离JmsTemplate并使用 JMS获得命名空间.

I'm not sure I get what you're trying to do. If you're trying to have multiple messages processed concurrently, get away from the JmsTemplate and use a DefaultMessageListenerContainer with concurrentConsumers. Also available via the JMS namespace.

例如,看来您可以丢弃问题中显示的所有代码,而改用此代码:

For example, it seems that you could throw away all the code that you're showing in your question and use this instead:

<jms:listener-container concurrency="10">
    <jms:listener destination="some.queue" ref="fooService" method="handleNewFoo"/>
</jms:listener-container>

这将自动产生多达10个线程用于并发消息处理.收到消息时,它将使用工作线程之一调用fooService.handleNewFoo(),其中fooService是您的Spring上下文中的bean.

That will automatically spawn up to 10 threads for concurrent message processing. When a message comes in, it will use one of the worker threads to call fooService.handleNewFoo(), where fooService is a bean in your Spring context.

编辑:我已经在github上创建了一个示例项目,其中显示了基本的Spring JMS设置.您可以在 https://github.com/zzantozz/testbed上浏览源. /tree/master/basic-spring-jms 或只是克隆并运行它:

I've created a sample project on github showing a basic Spring JMS setup. You can browse the source at https://github.com/zzantozz/testbed/tree/master/basic-spring-jms or just clone and run it:

git clone git://github.com/zzantozz/testbed.git tmp
cd tmp
mvn compile exec:java -Dexec.mainClass=rds.jms.Main -pl basic-spring-jms

有一个主类可以启动JMS代理并启动Spring.当Spring启动时,它会初始化一个开始发送JMS消息的bean.如上所述,还有一个Spring消息侦听器,它使用消息并将消息传递给产生消息的同一个bean,然后将它们打印到stdout.

There's a main class that starts a JMS broker and starts Spring. When Spring starts, it inits a bean that begins sending JMS messages. There's also a Spring message listener as I described above that consumes messages and passes them to the same bean that produces messages, which prints them to stdout.

这篇关于JMS和ThreadPool有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆