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

org.hornetq.api.core.client.ClientRequestor Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 32.0.0.Final
Show newest version
/*
 * Copyright 2005-2014 Red Hat, Inc.
 * Red Hat licenses this file to you 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.
 */
package org.hornetq.api.core.client;

import java.util.UUID;

import org.hornetq.api.core.SimpleString;
import org.hornetq.core.client.impl.ClientMessageImpl;

/**
 * The ClientRequestor class helps making requests.
 * 
* The ClientRequestor constructor is given a ClientSession and a request address. * It creates a temporary queue for the responses and provides a request method that sends the request message and waits for its reply. * * @apiviz.uses org.hornetq.api.core.client.ClientSession * @apiviz.owns org.hornetq.api.core.client.ClientProducer * @apiviz.owns org.hornetq.api.core.client.ClientConsumer * * @author Jeff Mesnil */ public final class ClientRequestor { private final ClientSession queueSession; private final ClientProducer requestProducer; private final ClientConsumer replyConsumer; private final SimpleString replyQueue; /** * Constructor for the ClientRequestor. * * The implementation expects a ClientSession with automatic commits of sends and acknowledgements * * @param session a ClientSession uses to handle requests and replies * @param requestAddress the address to send request messages to * @throws Exception */ public ClientRequestor(final ClientSession session, final SimpleString requestAddress) throws Exception { queueSession = session; requestProducer = queueSession.createProducer(requestAddress); replyQueue = new SimpleString(requestAddress + "." + UUID.randomUUID().toString()); queueSession.createTemporaryQueue(replyQueue, replyQueue); replyConsumer = queueSession.createConsumer(replyQueue); } /** * @see ClientRequestor#ClientRequestor(ClientSession, SimpleString) */ public ClientRequestor(final ClientSession session, final String requestAddress) throws Exception { this(session, SimpleString.toSimpleString(requestAddress)); } /** * Sends a message to the request address and wait indefinitely for a reply. * The temporary queue is used for the REPLYTO_HEADER_NAME, and only one reply per request is expected * * @param request the message to send * @return the reply message * @throws Exception */ public ClientMessage request(final ClientMessage request) throws Exception { return request(request, 0); } /** * Sends a message to the request address and wait for the given timeout for a reply. * The temporary queue is used for the REPLYTO_HEADER_NAME, and only one reply per request is expected * * @param request the message to send * @param timeout the timeout to wait for a reply (in milliseconds) * @return the reply message or {@code null} if no message is replied before the timeout elapses * @throws Exception */ public ClientMessage request(final ClientMessage request, final long timeout) throws Exception { request.putStringProperty(ClientMessageImpl.REPLYTO_HEADER_NAME, replyQueue); requestProducer.send(request); return replyConsumer.receive(timeout); } /** * Closes the ClientRequestor and its session. * * @throws Exception if an exception occurs while closing the ClientRequestor */ public void close() throws Exception { replyConsumer.close(); requestProducer.close(); queueSession.deleteQueue(replyQueue); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy