Where or when is it time to call the setMessageListener method on the TopicSubscriber?
I am trying to subscribe Topic
using TopicSubscriber
. For this TopicSubscriber
I am trying to assign MessageListener
. I have added the code below inside the init()
servlet method that is loaded when the server starts:
String destName = "topic/userManagementTopic";
TopicConnectionFactory connectionFactory = null;
TopicConnection connection = null;
TopicSession session = null;
Topic topic = null;
InitialContext jndiContext = BlaBla.magicallyBuildTheCorrectInitialContext("userManagementTopic");
connectionFactory = (TopicConnectionFactory)jndiContext.lookup("java:/JmsXA");
topic = (javax.jms.Topic)jndiContext.lookup(destName);
connection = (TopicConnection)connectionFactory.createConnection("topicUser", "topicPwd");
session = (TopicSession)connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.start();
TopicSubscriber recv = session.createSubscriber(topic);
recv.setMessageListener(new UserManagementTopicListener());
The problem is that I never go past the last line because I get
javax.jms.IllegalStateException: This method is not applicable inside the application server. See the J2EE spec
I tried to apply the recommended fix I found online by adding <config-property name="Strict" type="java.lang.Boolean">false</config-property>
to jms-ds.xml, but that doesn't fix it.
So how else can I subscribe to a topic and specify my desired one MessageListener
?
I am using jboss-5.1.0.GA and my application is Struts based.
Thanks for reading this.
Editing later. I ended up throwing an exception using "java:/ConnectionFactory"
for connection. Now this is even better, I have no errors and the Listener will not process messages. I only get messages when I add MDB declarations to it, but I don't want to use MDB. Now what?
Next edit 2: I did it by binding the listener like this:
String destName = "managementTopic";
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination dest = null;
MessageConsumer consumer = null;
ManagementTopicListener listener = null;
try {
ref = BlaBla.magicallyBuildTheCorrectInitialContext(destName);
jndiContext = ref.ctx;
connectionFactory = connectionFactory = (ConnectionFactory)jndiContext.lookup("XAConnectionFactory");
dest = (Topic)jndiContext.lookup("topic/" + destName);
connection = connectionFactory.createConnection(ref.getPrinciple(), ref.getCredentials());
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(dest);
listener = new ManagementTopicListener();
consumer.setMessageListener(listener);
connection.start();
//very important to never close this connection for as long as you want to receive messages
} catch (Exception e) {
System.out.println("kaboom " + e.getStackTrace());
if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
System.out.println("exc " + ex.getLinkedException());
}
}
}
source to share
No one has answered this question yet
Check out similar questions: