Reputation: 24411
I'm trying to create a temporary JMS queue using JMS 2.0 in Wildfly 10, and an injected connection factory.
I am injecting my ConnectionFactory using @JMSConnectionFactory. This works fine.
@Inject @JMSConnectionFactory("java:/jms/RemoteConnectionFactory") JMSContext jmsContext
Creating a temporary queue also works fine:
Destination jmsQueue = jmsContext.createTemporaryQueue();
Creating a publisher and publishing messages works okay as well:
JMSProducer producer = jmsContext.createProducer();
TextMessage msg = jmsContext.createTextMessage(Long.toString(new Date().getTime()));
producer.send(jmsQueue, msg);
However, how do I create a listener for the queue? I cannot use an MDB as the temp queue is not predefined. If I try to create a consumer, and assign a message listener to it, I get the following error message:
JMSConsumer consumer = jmsContext.createConsumer(jmsQueue);
consumer.setMessageListener(new MessageListener() {
...
...
});
Error trace:
Caused by: javax.jms.IllegalStateException: This method is not applicable inside the application server. See the J2EE spec, e.g. J2EE1.4 Section 6.6
at org.apache.activemq.artemis.ra.ActiveMQRASession.checkStrict(ActiveMQRASession.java:1452)
at org.apache.activemq.artemis.ra.ActiveMQRAMessageConsumer.setMessageListener(ActiveMQRAMessageConsumer.java:123)
at org.apache.activemq.artemis.jms.client.ActiveMQJMSConsumer.setMessageListener(ActiveMQJMSConsumer.java:59)
So it appears that I cannot explicity set a message listener with a JEE controlled connection factory. But given that it is a temp queue, I cannot create an MDB since the Destination is not known at compile time. So how do I listen to a temp queue?
Upvotes: 2
Views: 1160
Reputation: 17455
I was only able to solve this problem by using JMS 1.0. I had code something like:
TopicConnectionFactory topicConnectionFactory;
Topic topic;
TopicConnection topicConnection;
try {
InitialContext context = new InitialContext();
topicConnectionFactory = (TopicConnectionFactory)jndi.lookup("jboss/DefaultJMSConnectionFactory");
topic = (Topic)jndi.lookup("jms/myTopicName");
topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber topicSubscriber = topicSession.createSubscriber(projectTopic, null, false);
topicSubscriber.setMessageListener(listenerClass);
topicConnection.start();
}
...
where listenerClass
is a class that implements javax.jms.MessageListener
.
This takes advantage of the predefined JMS connection factory defined in Wildfly within standalone-full.xml
so that I don't need to set up an explicit one.
As a warning - the last I ran this code was in Wildfly 8 so some things may have changed a bit. Additionally, I wasn't using remote connections so, again, there may be some differences.
Upvotes: 1