我尝试使用TopicBrowser
浏览主题中的邮件。要调用将返回createBrowser
对象的正确TopicBrowser
方法,我需要将topicSession
变量投射到AQjmsSession
。
private InitialContext initialContext;
private TopicConnection topicConnection;
private TopicSession topicSession;
private Topic topic;
private TopicConnectionFactory topicFactory;
private TextMessage message;
private void initTopic(Context context, String topicName) throws NamingException, JMSException {
System.out.println("initializing the queue...");
topicFactory = (TopicConnectionFactory) context.lookup(JMS_FACTORY);
topicConnection = topicFactory.createTopicConnection();
//queueConnection.setExceptionListener(this);
topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("session created, lookup queue: " + topicName);
topic = (Topic)context.lookup(topicName);
System.out.println("done...");
}
private void readTopicMessage() throws JMSException {
int counter = 0;
System.out.println("Starting the subscriber");
TopicBrowser topicBrowser = ((AQjmsSession) topicSession).createBrowser(topic, SUBSCRIBER);
Enumeration msgs = topicBrowser.getEnumeration();
//topicConnection.start();
System.out.println("Topic started\n");
if (!msgs.hasMoreElements()) {
System.out.println("No messages in topic");
} else {
while (msgs.hasMoreElements()) {
System.out.println(">>> message count: " + ++counter);
Message message = (Message) msgs.nextElement();
System.out.println("MessageID: " + message.getJMSMessageID() + "\tPriority: " + message.getJMSPriority() + "\tDeliveryMode: " + message.getJMSDeliveryMode());
System.out.println("Timestamp: " + message.getJMSTimestamp() + "\tJMSDestination: " + message.getJMSDestination() + "\tReplyTo: " + message.getJMSReplyTo());
if (message instanceof TextMessage)
System.out.println("Data: " + ((TextMessage) message).getText());
System.out.println("\n");
if (counter >= 5) break;
}
}
System.out.println("stopping the topic");
topicConnection.stop();
}
尝试进行此演员会给我这个错误:
java.lang.ClassCastException: weblogic.jms.client.WLSessionImpl cannot be cast to oracle.jms.AQjmsSession
答案 0 :(得分:1)
您无法将其强制转换为该类型,因为您的应用程序所使用的客户端并非来自您认为的提供程序。根据例外情况,TopicSession是WebLogic JMS客户端提供的类型,因此强制转换注定会失败。您应该查看您的JNDI配置,看看它是否按照您认为配置的方式进行配置。
您的JNDI属性告诉代码从哪里创建ConnectionFactory并且获取WebLogic客户端位,您似乎需要Oracle AQ位,因此在JNDI步骤中查找的ConnectionFactory需要是Oracle的。