我使用带有 MQ 系列的 Websphere Application Server 8 作为消息队列。
当我以“postConstruct”方法打开会话bean中的连接并在另一种方法中使用它时,它会关闭。我的代码是:
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)
您知道连接关闭的原因吗?