public QueueConsumer createConsumer(String selector) throws ActiveMQException { String genId = sessionCounter.getAndIncrement() + "-queue-" + destination + "-" + startup; QueueConsumer consumer = new QueueConsumer(sessionFactory, destination, genId, serviceManager, selector); addConsumer(consumer); return consumer; }
public QueueConsumer(ClientSessionFactory factory, String destination, String id, DestinationServiceManager serviceManager, String selector) throws ActiveMQException { this.factory = factory; this.destination = destination; this.id = id; this.serviceManager = serviceManager; this.selector = selector; createSession(); }
private void headAutoAckSubscriptionResponse(UriInfo uriInfo, QueueConsumer consumer, Response.ResponseBuilder builder, String pathToPullSubscriptions) { // we synchronize just in case a failed request is still processing synchronized (consumer) { QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, pathToPullSubscriptions + "/acknowledged/" + consumer.getId(), Long.toString(consumer.getConsumeIndex())); } }
@Override public boolean testTimeout(String target, boolean autoShutdown) { QueueConsumer consumer = queueConsumers.get(target); if (consumer == null) return false; if (System.currentTimeMillis() - consumer.getLastPingTime() > consumerTimeoutSeconds * 1000) { ActiveMQRestLogger.LOGGER.shutdownRestConsumer(consumer.getId()); if (autoShutdown) { shutdown(consumer); } return true; } else { return false; } }
protected Response pollWithIndex(long wait, UriInfo info, String basePath, long index) { try { ClientMessage message = receive(wait); if (message == null) { Response.ResponseBuilder builder = Response.status(503).entity("Timed out waiting for message receive.").type("text/plain"); setPollTimeoutLinks(info, basePath, builder, Long.toString(index)); return builder.build(); } previousIndex = index; lastConsumed = ConsumedMessage.createConsumedMessage(message, this.getJmsOptions()); String token = Long.toString(lastConsumed.getMessageID()); Response response = getMessageResponse(lastConsumed, info, basePath, token).build(); if (autoAck) message.acknowledge(); return response; } catch (Exception e) { Response errorResponse = Response.serverError().entity(e.getMessage()).status(Response.Status.INTERNAL_SERVER_ERROR).build(); return errorResponse; } }
protected Response checkIndexAndPoll(long wait, UriInfo info, String basePath, long index) { ping(wait); if (lastConsumed == null && index > 0) { return Response.status(412).entity("You are using an old consume-next link and are out of sync with the JMS session on the server").type("text/plain").build(); } if (lastConsumed != null) { if (index == previousIndex) { String token = Long.toString(lastConsumed.getMessageID()); return getMessageResponse(lastConsumed, info, basePath, token).build(); } if (index != lastConsumed.getMessageID()) { return Response.status(412).entity("You are using an old consume-next link and are out of sync with the JMS session on the server").type("text/plain").build(); } } try { return pollWithIndex(wait, info, basePath, index); } finally { ping(0); // ping again as we don't want wait time included in timeout. } }
if (autoAck) { headAutoAckSubscriptionResponse(uriInfo, consumer, builder, pathToPullSubscriptions); consumer.setSessionLink(builder, uriInfo, pathToPullSubscriptions + "/auto-ack/" + consumer.getId()); } else { headAcknowledgedConsumerResponse(uriInfo, (AcknowledgedQueueConsumer) consumer, builder); consumer.setSessionLink(builder, uriInfo, pathToPullSubscriptions + "/acknowledged/" + consumer.getId()); queueConsumers.put(consumer.getId(), consumer); serviceManager.getTimeoutTask().add(this, consumer.getId()); else location.path("acknowledged"); location.path(consumer.getId()); Response.ResponseBuilder builder = Response.created(location.build()); if (autoAck) { QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/auto-ack/" + consumer.getId(), "-1"); } else { AcknowledgedQueueConsumer.setAcknowledgeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/acknowledged/" + consumer.getId(), "-1");
UriBuilder location = uriInfo.getAbsolutePathBuilder(); location.path(attributesSegment); location.path(consumer.getId()); Response.ResponseBuilder builder = Response.created(location.build()); QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1"); } else { AcknowledgedQueueConsumer.setAcknowledgeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1");
@Path("consume-next{index}") @POST public synchronized Response poll(@HeaderParam(Constants.WAIT_HEADER) @DefaultValue("0") long wait, @PathParam("index") long index, @Context UriInfo info) { ActiveMQRestLogger.LOGGER.debug("Handling POST request for \"" + info.getRequestUri() + "\""); if (closed) { UriBuilder builder = info.getBaseUriBuilder(); builder.path(info.getMatchedURIs().get(1)).path("consume-next"); String uri = builder.build().toString(); // redirect to another consume-next return Response.status(307).location(URI.create(uri)).build(); } return checkIndexAndPoll(wait, info, info.getMatchedURIs().get(1), index); }
protected Response pollWithIndex(long wait, UriInfo info, String basePath, long index) { try { ClientMessage message = receive(wait); if (message == null) { Response.ResponseBuilder builder = Response.status(503).entity("Timed out waiting for message receive.").type("text/plain"); setPollTimeoutLinks(info, basePath, builder, Long.toString(index)); return builder.build(); } previousIndex = index; lastConsumed = ConsumedMessage.createConsumedMessage(message, this.getJmsOptions()); String token = Long.toString(lastConsumed.getMessageID()); Response response = getMessageResponse(lastConsumed, info, basePath, token).build(); if (autoAck) message.acknowledge(); return response; } catch (Exception e) { Response errorResponse = Response.serverError().entity(e.getMessage()).status(Response.Status.INTERNAL_SERVER_ERROR).build(); return errorResponse; } }
protected Response checkIndexAndPoll(long wait, UriInfo info, String basePath, long index) { ping(wait); if (lastConsumed == null && index > 0) { return Response.status(412).entity("You are using an old consume-next link and are out of sync with the JMS session on the server").type("text/plain").build(); } if (lastConsumed != null) { if (index == previousIndex) { String token = Long.toString(lastConsumed.getMessageID()); return getMessageResponse(lastConsumed, info, basePath, token).build(); } if (index != lastConsumed.getMessageID()) { return Response.status(412).entity("You are using an old consume-next link and are out of sync with the JMS session on the server").type("text/plain").build(); } } try { return pollWithIndex(wait, info, basePath, index); } finally { ping(0); // ping again as we don't want wait time included in timeout. } }
if (autoAck) { headAutoAckSubscriptionResponse(uriInfo, consumer, builder, pathToPullSubscriptions); consumer.setSessionLink(builder, uriInfo, pathToPullSubscriptions + "/auto-ack/" + consumer.getId()); } else { headAcknowledgedConsumerResponse(uriInfo, (AcknowledgedQueueConsumer) consumer, builder); consumer.setSessionLink(builder, uriInfo, pathToPullSubscriptions + "/acknowledged/" + consumer.getId()); queueConsumers.put(consumer.getId(), consumer); serviceManager.getTimeoutTask().add(this, consumer.getId()); else location.path("acknowledged"); location.path(consumer.getId()); Response.ResponseBuilder builder = Response.created(location.build()); if (autoAck) { QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/auto-ack/" + consumer.getId(), "-1"); } else { AcknowledgedQueueConsumer.setAcknowledgeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/acknowledged/" + consumer.getId(), "-1");
@Override public boolean testTimeout(String target, boolean autoShutdown) { QueueConsumer consumer = queueConsumers.get(target); if (consumer == null) return false; if (System.currentTimeMillis() - consumer.getLastPingTime() > consumerTimeoutSeconds * 1000) { ActiveMQRestLogger.LOGGER.shutdownRestConsumer(consumer.getId()); if (autoShutdown) { shutdown(consumer); } return true; } else { return false; } }
UriBuilder location = uriInfo.getAbsolutePathBuilder(); location.path(attributesSegment); location.path(consumer.getId()); Response.ResponseBuilder builder = Response.created(location.build()); QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1"); } else { AcknowledgedQueueConsumer.setAcknowledgeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1");
@Path("consume-next{index}") @POST public synchronized Response poll(@HeaderParam(Constants.WAIT_HEADER) @DefaultValue("0") long wait, @PathParam("index") long index, @Context UriInfo info) { ActiveMQRestLogger.LOGGER.debug("Handling POST request for \"" + info.getRequestUri() + "\""); if (closed) { UriBuilder builder = info.getBaseUriBuilder(); builder.path(info.getMatchedURIs().get(1)).path("consume-next"); String uri = builder.build().toString(); // redirect to another consume-next return Response.status(307).location(URI.create(uri)).build(); } return checkIndexAndPoll(wait, info, info.getMatchedURIs().get(1), index); }
private void headAutoAckSubscriptionResponse(UriInfo uriInfo, QueueConsumer consumer, Response.ResponseBuilder builder, String pathToPullSubscriptions) { // we synchronize just in case a failed request is still processing synchronized (consumer) { QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, pathToPullSubscriptions + "/acknowledged/" + consumer.getId(), Long.toString(consumer.getConsumeIndex())); } }