我的应用程序使用嵌入了TomEE Plus-8.0.0-M1的ActiveMQ。

耗尽池连接的行为是相同的,而不管使用使用resources.xml的显式定义的javax.jms.ConnectionFactory还是让TomEE即时生成一个。

连接超时的不同设置似乎没有效果:

connectionMaxIdleTime = 3 seconds
connectionMaxIdleTime = 15 minutes


如果ExampleSessionBean.sendMessage()被顺序调用11次,则“废弃的连接”警告将记录在消息1-10上。在消息#11上,抛出JMSRuntimeException:没有可用的托管连接。

然后,如果我等待几分钟,JMSContext生产者将能够再次发送。

我首先想到的是,需要使用以下命令关闭JMSContext中的基础连接

jmsContext.close();


但来自Interface JMSContext

“如果JMSContext是容器管理的(注入),则不得使用此方法。”

这里需要哪些编程或配置更改?

谢谢,
特德

resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <Resource id="jmsConnectionFactory" type="javax.jms.ConnectionFactory">
        connectionMaxIdleTime = 3 seconds
        connectionMaxWaitTime = 5 seconds
        poolMaxSize = 10
        poolMinSize = 0
        resourceAdapter = Default JMS Resource Adapter
        transactionSupport = xa
    </Resource>
</resources>


ExampleSessionBean.java

@Named
@LocalBean
@Stateless
public class ExampleSessionBean
{

    @Resource(name = "jms/localNotificationQueue")
    private Queue _localNotificationQueue;

    @Inject
    @JMSConnectionFactory("jmsConnectionFactory")
    private JMSContext _jmsContext;

    public void sendMessage(String message)
    {
        try
        {
            TextMessage textMessage =
                _jmsContext.createTextMessage(message);
            _jmsContext.createProducer().
                setDeliveryMode(DeliveryMode.PERSISTENT).
                send(_localNotificationQueue, textMessage);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


警告-消息1-10 :(请原谅格式化。SE格式化程序由于某种原因不会格式化此文本块)

开始警告文字

警告:事务已完成,但是连接仍然具有关联的句柄:ManagedConnectionInfo:
... org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension $ InternalJMSContext.createProducer(JMS2CDIExtension.java:370),com.myorg.jms.ExampleSessionBean.sendMessage(ExampleSessionBean.java:46),

...

结束警告文字

讯息#11异常

javax.jms.JMSRuntimeException: No ManagedConnections available within configured blocking timeout ( 5000 [ms] ) for pool org.apache.geronimo.connector.outbound.SinglePoolConnectionInterceptor@12aff7fa
    at org.apache.openejb.resource.activemq.jms2.JMS2.toRuntimeException(JMS2.java:83)
    at org.apache.openejb.resource.activemq.jms2.JMSContextImpl.connection(JMSContextImpl.java:85)
    at org.apache.openejb.resource.activemq.jms2.JMSContextImpl.session(JMSContextImpl.java:102)
    at org.apache.openejb.resource.activemq.jms2.JMSContextImpl.getInnerProducer(JMSContextImpl.java:124)
    at org.apache.openejb.resource.activemq.jms2.JMSContextImpl.createProducer(JMSContextImpl.java:302)
    at org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension$InternalJMSContext.createProducer(JMS2CDIExtension.java:370)
    at com.myorg.jms.ExampleSessionBean.sendMessage(ExampleSessionBean.java:46)
...


更新:此行为已被复制并添加到TomEE issues tracker

最佳答案

我放弃了JMSContext并回到手动管理连接。即使加载了数百条消息,所有功能也会按预期运行。

ExampleSessionBean.java

@Named
@LocalBean
@Stateless
public class ExampleSessionBean
{
    @Resource(name = "jms/localNotificationQueue")
    private Queue _localNotificationQueue;

    @Resource(mappedName = "jmsConnectionFactory")
    private ConnectionFactory _connectionFactory;

    public void sendMessage(String message)
    {
        Connection connection = null;
        Session session = null;
        MessageProducer messageProducer = null;
        try
        {
            connection =
                    _connectionFactory.createConnection();
            connection.start();
            session =
                    connection.createSession();
            messageProducer =
                    session.createProducer(null);
            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
            TextMessage textMessage = session.createTextMessage(message);
            messageProducer.send(_localNotificationQueue,
                    textMessage);
        }
        catch (Exception e)
        {
            // TODO Handle exception
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (JMSException e) {}
            }
        }
    }
}

10-08 12:08