以下是使用持久订阅的Java代码
private void execute()throws Exception {
logger.debug("Creating JNDI context");
Properties jndiProps = new Properties();
jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, config.getJndiFactory());
jndiProps.setProperty(Context.PROVIDER_URL, config.getJndiProviderUrl());
jndiProps.setProperty(Context.SECURITY_CREDENTIALS, config.getJndiSecurityCredential());
jndiProps.setProperty(Context.SECURITY_PRINCIPAL, config.getJndiSecurityPrincipal());
logger.debug("JNDI properties: " + jndiProps.toString());
jndiContext = new InitialContext(jndiProps);
logger.debug("JNDI context created successfully");
logger.debug("Looking up ConnectionFactory : " + config.getJmsConnFactory());
TopicConnectionFactory connFactory = (TopicConnectionFactory) jndiContext.lookup(config.getJmsConnFactory());
logger.debug("Creating connection object");
conn = connFactory.createTopicConnection(config.getJmsUserName(), config.getJmsPasswd());
logger.debug("Connection object successfully created");
TopicSession session = null;
MessageConsumer subscriber = null;
try {
session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
logger.debug("Starting connection");
conn.start();
logger.debug("Getting destinaton");
Topic topic = (Topic)jndiContext.lookup(config.getDestination());
if (topic == null) {
throw new RuntimeException("Invalid destination");
}
logger.debug("Creating durable subscriber");
if(config.isDurable()){
subscriber = session.createDurableSubscriber(topic, config.getSubscriberId());
}else{
subscriber = session.createSubscriber(topic);
}
boolean runFlag = true;
do {
logger.debug("Receiving messages...");
TextMessage message = (TextMessage) subscriber.receive(DEF_TIMEOUT_MILLIS);
if (message != null) {
logger.debug("Received message : " + message.getText());
continue;
}
logger.debug("No available messages now");
runFlag = false;
} while (runFlag);
logger.debug("There is no more messages available. Exiting...");
} finally {
if(config.isDurable()){
session.unsubscribe(config.getSubscriberId());
}
close(subscriber);
close(session);
}
}
执行时我收到“TCPLink错误:消息中的魔法无效”。之后,会话和连接将自动终止。
javax.jms.IllegalStateException:会话已关闭
javax.jms.JMSException:连接已终止
请帮忙。
提前致谢