JMS MQ Connection在JSF 2 SessionBean中关闭

时间:2013-10-24 06:20:00

标签: java jsf-2 jms mq

我使用带有MQ Series的Websphere Application Server 8作为消息队列。

当我在“postConstruct”方法中打开sessionbean中的连接时,我在另一个方法中使用它然后关闭它。我的代码是:

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

@ManagedBean
@SessionScoped
public class MQRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @Resource(name = "jms/wasmqtest/wasmqtest_QCF")
    private QueueConnectionFactory connectionFactory;

    @Resource(name = "jms/wasmqtest/Request_Q")
    private Queue requestQueue;

    private QueueConnection connection;

    private String text = "";

    public void sendMessage() {
        System.out.println("Connection in sendMessage: \n" + connection);
        TextMessage msg;
        try {
            QueueSession queueSession = connection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            QueueSender sender = queueSession.createSender(requestQueue);
            msg = queueSession.createTextMessage(text);

            sender.send(msg);

            queueSession.close();
            sender.close();

        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        text = "";

    }

    @PostConstruct
    public void openConenction() {
        System.out.println("Open Connection");
        try {
            connection = connectionFactory.createQueueConnection();

            connection.start();
            System.out.println("Connection in OpenConnectioN: \n" + connection);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void closeConnection() {
        try {
            System.out.println("Closing Connection");
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

在PostConstruct方法中初始化连接:

[21.10.13 07:36:05:574 CEST] 00000025 SystemOut     O Connection in OpenConnectioN: 
com.ibm.ejs.jms.JMSQueueConnectionHandle@36c9b1a
    managed connection = com.ibm.ejs.jms.JMSManagedQueueConnection@3657e8b
    physical connection = com.ibm.mq.jms.MQXAQueueConnection@36618b6
    closed = false
    invalid = false
    restricted methods enabled = false
    open session handles = []
    temporary queues = []

但是在sendMessage()方法中它不是,我得到一个ConnectionClosed问题:

[21.10.13 07:36:12:493 CEST] 00000025 SystemOut     O Connection in sendMessage: 
com.ibm.ejs.jms.JMSQueueConnectionHandle@36c9b1a
    managed connection = null
    physical connection = null
    closed = true
    invalid = false
    restricted methods enabled = false
    open session handles = []
    temporary queues = []

21.10.13 07:36:12:461 CEST] 00000025 SystemErr     R 15 [WebContainer : 3] INFO org.apache.bval.jsr303.ConfigurationImpl - ignoreXmlConfiguration == true
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R javax.jms.IllegalStateException: Connection closed
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at com.ibm.ejs.jms.JMSConnectionHandle.checkOpen(JMSConnectionHandle.java:821)
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at com.ibm.ejs.jms.JMSQueueConnectionHandle.createQueueSession(JMSQueueConnectionHandle.java:206)
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at de.volkswagen.wasmqtest.queue.MQRequest.sendMessage(MQRequest.java:51)
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[21.10.13 07:36:12:601 CEST] 00000025 SystemErr     R   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at java.lang.reflect.Method.invoke(Method.java:611)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component.UICommand.broadcast(UICommand.java:120)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:973)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:275)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1285)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:711)
[21.10.13 07:36:12:602 CEST] 00000025 SystemErr     R   at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1020)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:953)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1655)
[21.10.13 07:36:12:603 CEST] 00000025 SystemErr     R   at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[21.10.13 07:36:12:604 CEST] 00000025 SystemErr     R   at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[21.10.13 07:36:12:605 CEST] 00000025 SystemErr     R   at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1650)

您知道连接关闭的原因吗?

1 个答案:

答案 0 :(得分:3)

根据this IBM technote,似乎自WAS 6以来,您应该在每个MQ会话的基础上打开和关闭连接。因为,当您关闭会话时,负责创建MQ会话的基础连接将隐式返回到池中。

  

技术说明(故障排除)

     

问题(摘要)

     

您可以从位于WebSphere Application Server中的EJB bean中的Java™应用程序使用JMS访问WebSphere MQ V6.0队列。在WebSphere Application Server V5中,该应用程序已运行多年而没有任何错误。您现在正尝试在WebSphere Application Server V6上运行完全相同的Java应用程序。

     

...

     

ffdc日志中失败方法堆栈的相关部分显示:

javax.jms.IllegalStateException: Connection closed
    at com.ibm.ejs.jms.JMSConnectionHandle.checkOpen(JMSConnectionHandle.java:671) 
    at com.ibm.ejs.jms.JMSQueueConnectionHandle.createQueueSession(JMSQueueConnectionHandle.java:172)
    ... 
     

...

     

原因

     

genericPut方法完成时,从池中检索的连接句柄与应用程序分离并返回到JMS连接池...

     

...

     

解决问题

     
      
  1. 每次调用EJB时​​,修改应用程序以创建新的JMS连接。这是推荐的解决方案。

         

    由于应用服务器的连接池机制的工作方式,这对性能没有太大影响。

  2.         

    ...

因此,在您的特定情况下,正确的方法如下。想象try-finally就像在JDBC中一样!您只需要将会话/发件人明确关闭为they are already implicitly closed on close of a connection

public void sendMessage() throws JMSException {
    QueueConnection connection = null;

    try {
        connection = connectionFactory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(requestQueue);
        TextMessage message = session.createTextMessage(text);
        sender.send(message);
    } finally {
        if (connection != null) try {
            connection.close();
        } catch (JMSException e) {
            // Log or ignore.
        }
    }
}