I'm trying to develop a simple application with a publisher and a subscriber.
To publish a message I wrote che following instructions:
Properties env = new Properties();
Context ctx = new InitialContext(env);
tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup("jms/RemoteConnectionFactory"),TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup("jms/"+topicName), Topic.class);
tpublisher = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
System.out.println("Following Messages has been sent !!!");
msg.setText(""+j); // Messages
tpublisher.publish(msg); // Messages sent
System.out.println("Message Sent = "+j);
When I run these instructions I get no error but I cannot see any message from JBoss Administration Console. Is there any error?
Your code looks fine as far as I can tell. Although you should try to use ConnectionFactory, Session, Destination, and MessageProducer rather than TopicConnectionFactory, TopicSession, Topic, and TopicPublisher respectively since that is the recommendation from the JMS spec.
Jeff is right. This is basic JMS semantics with topics.
Try using a queue rather than a topic.
Using a topic is fine as long as you understand the basic semantics (i.e. if there are no subscriptions any message sent to the topic will be discarded).
The "unified" interfaces are the ones I referred to in my previous comment. To be clear, these are unified:
These are not unified:
- QueueConnectionFactory, TopicConnectionFactory
- QueueSession, TopicSession
- Queue, Topic
- QueueSender, TopicPublisher