Question

I use Websphere Application Server 8 with MQ Series as Messaging Queue.

When I open close the connection in sessionbean in a "postConstruct" method and I use it in another method then its closed. My Code is:

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

In PostConstruct method the connection is initialized:

[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 = []

But in sendMessage() method it isnt and I get a ConnectionClosed Problem:

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

Do you have an idea why the connection is closed?

Was it helpful?

Solution

As per this IBM technote it appears that since WAS 6 you're supposed to open and close the connection on a per-MQ-session basis. Because, when you close a session, the underlying connection responsible for creating the MQ session is implicitly returned to the pool.

Technote (troubleshooting)

Problem(Abstract)

You access WebSphere MQ V6.0 queues using JMS from a Java™ application which sits in EJB beans in WebSphere Application Server. The application has been running for a number of years with no errors, in WebSphere Application Server V5. You are now trying to run exactly the same Java application on WebSphere Application Server V6.

...

The relevant section of the failing method stack from the ffdc log shows:

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

...

Cause

When the genericPut method completes, then a connection handle which was retrieved from the pool is dissociated from the application and returned to the JMS connection pool ...

...

Resolving the problem

  1. Modify the application to create a new JMS connection every time the EJB is called. This is the recommended solution.

    This will not have too much impact on performance, due to the way the application server's connection pooling mechanism works.

...

So, in your particular case, the proper approach would be as follows. Think of try-finally like as in JDBC! You only don't need to explicitly close the session/sender as 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.
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top