I'm not 100% sure what you're problem actually is but it sounds to me like you need to use durable subscribers!
I would like to as a follow on question relating to durable subscriptions. If a durable subscription is created:
MessageConsumer consumer = session.createDurableSubscriber(Topic topic, String name);
The whole point of using durable subscribers is so that messages can be received while the consumer is not connected. Let us say the consumer above is created in a method and the reference ceases to exist after. The consumer can still be notified of any messages published to its topic. However, how can that consumer be closed and unsubscribed? If you attempt to unsubscribe the durable subscriber:
an IllegalStateException will be thrown per the JMS 1.1 spec:
"It is erroneous for a client to delete a durable subscription while there is an active MessageConsumer or TopicSubscriber for the subscription, or while a consumed message is part of a pending transaction or has not been acknowledged in the session. "
How then can durable subscriptions ever be deleted? I understand if you create a new one with the same name and a new topic, it will effectively delete the old one. Is this the only option?
No, that's not how JMS durable subscriptions work. There are quite a few good JMS tutorials out there which should go through this.
MessageConsumer consumer = session.createDurableSubscriber(myTopic, "foo"); //Creates the durable subscription and returns a consumer on it consumer.close() //Closes the consumer but the subscription is still active and receiving messages - you can close down the whole client at this point.. then.. some time later: consumer = session.createDurableSubscriber(myTopic, "foo"); //Does not create a new durable sub since it already exists - but just returns a consuimer on it. consumer.close() // closes the consumer - the subscription is still alive session.unsubscribe("foo") // that's how you delete it - notice you've already closed the consumer/
Please forgive me for posting what may be trivial questions but I have not found the answers to any questions that I have on the web.
Using the above approach, the consumer that takes active control of the subscription would have to poll to retrieve the messages published while it was offline ( consumer.receive() )? When disconnected, the MessageListener's asynchronous nature is eliminated.
I am trying to accomplish something pretty simply but I have yet to find a simple solution. I would like to create message consumers for various topics. The message consumers will be created upon an HTTP request (the socket will be closed and will not wait). So, a particular request could generate a couple of message consumers. What is the correct way to manage them, durable non-durable? If non-durable, store the message consumers in memory?
As you have noticed, I am a JMS newbie and am not aware of existing patterns. If you could provide me with external links or examples, that would be very helpful.
MessageConsumer.setMessageListener() will always there for you to receive async messages.
Durable means JBM server will keep the messages for a durable subscription until the messages are received or expired. Please see the JMS spec for details.
I think the problem you have is you're creating state from a http request. The http request/response paradigm is stateless and shouldn't be spawning any threads etc. you can add objects to a http session and access them from within a servlet but these are tied to the session. Maybe you need a re think for your application.
I am trying to accomplish something similar to bdaye4.
I have multiple clients to whom I want to publish updates from my server.
So, whenever my server has an update it publishes to a Topic XYZ.
What I want to achive is :
Whenever a client is started, I want it to subscribe to the Topic XYZ.
Whenever a client is stopped, I want it to unsubscibe from the topic XYZ.
Option I : Using Durable Subscribers:
When a client is created:
I use TopicSession to create Durable subscribers so that I can specify the subscriptionID with which I want to create the subscriber.
When a client is stopped:
I create a TopicSession on the ConnnectionFactory.
I will need to call TopicSession.unsubscribe(subscriptionID), but this gives me IllegalStateException saying that the subscriber is still active.
Can somebody let me know, if I am missing something(It's a Friday).
Option II : Non-Durable
Alternatively, if I use non-durable subscription,
how do create non-durable subscribers with a subscriptionID, so that I can remove those subscribers by specifying a subscriptionID.
Any help is appreciated.
I can dynamically create new topics by using the ServerPeer and MBeans.
Could you please share the code which injects or lookups ServerPeer please?
I can't inject it
@EJB(beanName = "jboss.messaging:service=ServerPeer") ServerPeer serverPeer;
due to "Caused by: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property"
Neither lookup it
MBeanServer server = MBeanServerLocator.locate(); serverPeer = (ServerPeer) MBeanProxyExt.create( ServerPeer.class, "jboss.messaging:service=ServerPeer",server);
because of "org.jboss.jms.server.ServerPeer is not an interface"
How to obtain reference which would allow to make: