1
votes

I want to use JMS (Topic) in my JavaEE 6 project. I have one class which acts as a publisher and subscriber of a topic at once. The following code shows the most important parts of the class.

public class MessageHandler implements MessageListener {
    private static TopicConnectionFactory factory;
    private static Topic topic;

    private TopicSubscriber subscriber;
    private TopicPublisher publisher;

    public MessageHandler() throws NamingException, JMSException {
            if (factory == null) {
                Context context = new InitialContext();
                factory = (TopicConnectionFactory) new InitialContext()
                        .lookup("jms/myfactory");
                topic = (Topic) context.lookup("jms/mytopic");
            }
            TopicConnection connection = factory.createTopicConnection();
            connection.start();
            TopicSession session = connection
                    .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            subscriber = session.createSubscriber(topic);
        }

    @Override
    public void onMessage(Message message) {
        try {
            ObjectMessage msg = (ObjectMessage) message;
            Object someO=  msg.getObject();
            System.out.println(this + " receives "+someO);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(Object someO) {
        try {
            ObjectMessage msg = session.createObjectMessage();
            msg.setObject(someO);
            publisher = session.createPublisher(topic);
         publisher.publish(msg);
         publisher.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }     
}

My question is, if this is a good way to design such a class. My idea was to share one connection and session for both subscribing and publishing. But I'm scared that this could lead to some overhead or blocking because I'm not closing the connection, session, subscriber and publisher until the object is not needed anymore. All examples I found online directly close everything after a message was sent or received...

Thanks in advance!

1

1 Answers

0
votes

Why do you want the class to be subscriber and publisher at once?

Whenever using a messaging system, you may well act as both, but why would you do it for the same topic, you surely don't want to receive your own messages?

So, the purpose of a topic, is to be used among several parts within an application or among several applications - one is placing a message into the topic and others receive the message they subscribed for.

And that also explains what you saw in the examples - the message processing is a one time thing, thus the connection can be closed afterwards.

By the way, since you ask this question within the "java-ee 6" area - can't you use a message driven bean, annotate your topic configuration and let the application server do the infrastructure part for you?