我们使用的是 JBoss EAP 6.2.4,在无状态会话 bean 中,我们将 JMS 消息发送到 WMQ-queuemanager。
我们的代码如下:
@Stateless
@LocalBean
public class MessageSenderBean {
private static ConnectionFactory connectionFactory;
private static InitialContext initialContext;
@EJB
IntegrationPropertyBean ipb;
Logger logger = Logger.getLogger(getClass());
/**
* Default constructor.
*/
public MessageSenderBean() {
}
@PostConstruct
public void postConstruct() {
logger.debug(" MessageSenderBeanPostConstruct called");
try {
initialContext = new InitialContext();
String connectionFactoryName = ipb.getProperty(
MessageSenderBean.class, "connectionFactory");
connectionFactory = (ConnectionFactory) initialContext
.lookup(connectionFactoryName);
} catch (NamingException e) {
logger.error("Exception occurred: " + e.toString());
logger.error(e);
}
}
public String sendMessage(String queueName, String content) {
String result = null;
Connection connection = null;
try {
connection = connectionFactory.createConnection();
} catch (JMSException e) {
logger.error("Exception occurred: " + e.toString());
logger.error(e);
}
// prüfen ob InitialContext leer
try {
if (initialContext == null)
initialContext = new InitialContext();
} catch (NamingException e) {
logger.error("Exception occurred: " + e.toString());
logger.error(e);
}
在服务器启动后,bean 可以完美地完成第一个操作,但一段时间后没有任何操作,bean 会丢失 initialContext 并且在 new InitialContext() 中的附加创建失败
知道为什么吗?
谢谢约尔格