简体   繁体   中英

Implementing asynchronous publish subscribe topic in tibco ems

my scenario is that me as a movie distributor, need to update my clients on new movies, I publish this information on a topic with durable subscribers and clients who want to buy the movie will express their interest.

However, this is where things go south, my implementation of the publisher stops listening as soon as it receives the first reply. Any help would be greatly appreciated. Thank you.

request(Message message) Sends a request and waits for a reply. The temporary topic is used for the JMSReplyTo destination; the first reply is returned, and any following replies are discarded.

https://docs.oracle.com/javaee/6/api/javax/jms/TopicRequestor.html

You want to continue reading messages in a loop. Here is an example:

    /* read messages */
    while (true)
    {
        /* receive the message */
        msg = msgConsumer.receive();
        if (msg == null)
           break;

        if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
            ackMode == Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE ||
            ackMode == Tibjms.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
        {
            msg.acknowledge();
        }

        System.err.println("Received message: "+ msg);
    }

You may want to also consider a possible issue with durable consumers. If your consumers never pick up their messages, storage will continue to grow at the server side. For this reason you may want to send your messages with an a expiration time, and/or limit maximum number of messages (or size in KB/MB/GB) of the JMS topics you are using.

First thing first... I have questions regarding the scenario. Is this some kind of test/exercice, or are we talking about a real world scenario ?

Are all client interested in the movie SEPARATE topic subscribers ? How does that scale ? I the plan to have a topic for every movie, and possible interested parties declaring durable subscribers (one each, for every movie) ? This seems to be abuse of durable subcribers... I would suggest using ONLY one subscriber (in system B) to a "Movie Released" event/topic (from system A), and have some code (in system B) reading all the clients from a DB to send emails/messages/whatever. (If system A and B are the same, it may or not be a good idea to use EMS at all... depends.)

If it is not an exercise, I must comment : Don't use a MOM (EMS, ActiveMQ) to do a DBMS' (Oracle, PostGreSQL) work !

With the disclaimer section done, I suggest an asynchronous subscription approach (These two clips are taken for the EMS sample directory. File tibjmsAsyncMsgConsumer.java ).

Extract from the constructor (The main class must implements ExceptionListener, MessageListener):

        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl);

        /* create the connection */
        connection = factory.createConnection(userName,password);

        /* create the session */
        session = connection.createSession();

        /* set the exception listener */
        connection.setExceptionListener(this);

        /* create the destination */
        if (useTopic)
            destination = session.createTopic(name);
        else
            destination = session.createQueue(name);

        System.err.println("Subscribing to destination: "+name);

        /* create the consumer */
        msgConsumer = session.createConsumer(destination);

        /* set the message listener */
        msgConsumer.setMessageListener(this);

        /* start the connection */
        connection.start();

The method is then called every time a message arrives.

public void onMessage(Message msg)
{
    try
    {
        System.err.println("Received message: " + msg);
    }
    catch (Exception e)
    {
        System.err.println("Unexpected exception in the message callback!");
        e.printStackTrace();
        System.exit(-1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM