使用JMSContext的TomEE Embedded ActiveMQ:在配置的阻止超时(5000 [ms])中没有ManagedConnections可用

时间:2019-03-24 18:03:50

标签: java jms activemq tomee-8 jmscontext

我的应用程序正在使用嵌入了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是容器管理的(注入),则不得使用此方法。”

这里需要对程序或配置进行哪些更改?

谢谢你, 泰德·S

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

1 个答案:

答案 0 :(得分:0)

我放弃了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) {}
            }
        }
    }
}