All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.jms.TopicRequestor Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2013 ScalAgent Distributed Technologies
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ---------------------------------------------------------------------
 *  $Id: TopicRequestor.java 6347 2013-03-13 08:52:02Z tachker $
 *  ---------------------------------------------------------------------
 */

package javax.jms;

/**
 * The {@code TopicRequestor} helper class simplifies making service requests.
 * 
 * @see javax.jms.QueueRequestor
 * 
 * @version JMS 2.0
 * @since JMS 1.0
 * 
 */

public class TopicRequestor {
    private TopicSession session;
    private TemporaryTopic temporaryTopic;
    private TopicPublisher publisher;
    private TopicSubscriber subscriber;

  /**
   * Constructor for the {@code TopicRequestor} class.
   * 
   * @param session
   *          the {@code TopicSession} the topic belongs to
   * @param topic
   *          the topic to perform the request/reply call on
   * 
   * @exception JMSException
   *              if the JMS provider fails to create the {@code TopicRequestor}
   *              due to some internal error.
   * @exception InvalidDestinationException
   *              if an invalid topic is specified.
   */

  public TopicRequestor(TopicSession session, Topic topic) throws JMSException {
      if (topic == null)
          throw new InvalidDestinationException("Invalid topic");
        this.session = session;
        temporaryTopic = session.createTemporaryTopic();
        publisher = session.createPublisher(topic);
        subscriber = session.createSubscriber(temporaryTopic);
  }

  /**
   * Sends a request and waits for a reply. The temporary topic is used for the
   * {@code JMSReplyTo} destination; the first reply is returned, and any
   * following replies are discarded.
   * 
   * @param message
   *          the message to send
   * 
   * @return the reply message
   * 
   * @exception JMSException
   *              if the JMS provider fails to complete the request due to some
   *              internal error.
   */

  public Message request(Message message) throws JMSException {
      message.setJMSReplyTo(temporaryTopic);
      publisher.publish(message);
      return subscriber.receive();
  }

  /**
   * Closes the {@code TopicRequestor} and its session.
   * 
   * 

* Since a provider may allocate some resources on behalf of a * {@code TopicRequestor} outside the Java virtual machine, clients should * close them when they are not needed. Relying on garbage collection to * eventually reclaim these resources may not be timely enough. * *

* Note that this method closes the {@code TopicSession} object passed to the * {@code TopicRequestor} constructor. * * @exception JMSException * if the JMS provider fails to close the {@code TopicRequestor} * due to some internal error. */ public void close() throws JMSException { session.close(); temporaryTopic.delete(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy