Search in sources :

Example 1 with AcknowledgedQueueConsumer

use of org.apache.activemq.artemis.rest.queue.AcknowledgedQueueConsumer in project activemq-artemis by apache.

the class SubscriptionsResource method createSubscription.

@POST
public Response createSubscription(@FormParam("durable") @DefaultValue("false") boolean durable, @FormParam("autoAck") @DefaultValue("true") boolean autoAck, @FormParam("name") String subscriptionName, @FormParam("selector") String selector, @FormParam("delete-when-idle") Boolean destroyWhenIdle, @FormParam("idle-timeout") Long timeout, @Context UriInfo uriInfo) {
    ActiveMQRestLogger.LOGGER.debug("Handling POST request for \"" + uriInfo.getPath() + "\"");
    if (timeout == null)
        timeout = Long.valueOf(consumerTimeoutSeconds * 1000);
    // default is true if non-durable
    boolean deleteWhenIdle = !durable;
    if (destroyWhenIdle != null)
        deleteWhenIdle = destroyWhenIdle.booleanValue();
    if (subscriptionName != null) {
        // see if this is a reconnect
        QueueConsumer consumer = queueConsumers.get(subscriptionName);
        if (consumer != null) {
            boolean acked = consumer instanceof AcknowledgedSubscriptionResource;
            acked = !acked;
            if (acked != autoAck) {
                throw new WebApplicationException(Response.status(412).entity("Consumer already exists and ack-modes don't match.").type("text/plain").build());
            }
            Subscription sub = (Subscription) consumer;
            if (sub.isDurable() != durable) {
                throw new WebApplicationException(Response.status(412).entity("Consumer already exists and durability doesn't match.").type("text/plain").build());
            }
            Response.ResponseBuilder builder = Response.noContent();
            String pathToPullSubscriptions = uriInfo.getMatchedURIs().get(0);
            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());
            }
            return builder.build();
        }
    } else {
        subscriptionName = generateSubscriptionName();
    }
    ClientSession session = null;
    try {
        // if this is not a reconnect, create the subscription queue
        if (!subscriptionExists(subscriptionName)) {
            session = sessionFactory.createSession();
            if (durable) {
                session.createQueue(destination, subscriptionName, true);
            } else {
                session.createTemporaryQueue(destination, subscriptionName);
            }
        }
        QueueConsumer consumer = createConsumer(durable, autoAck, subscriptionName, selector, timeout, deleteWhenIdle);
        queueConsumers.put(consumer.getId(), consumer);
        serviceManager.getTimeoutTask().add(this, consumer.getId());
        UriBuilder location = uriInfo.getAbsolutePathBuilder();
        if (autoAck)
            location.path("auto-ack");
        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");
        }
        return builder.build();
    } catch (ActiveMQException e) {
        throw new RuntimeException(e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (ActiveMQException e) {
            }
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) QueueConsumer(org.apache.activemq.artemis.rest.queue.QueueConsumer) AcknowledgedQueueConsumer(org.apache.activemq.artemis.rest.queue.AcknowledgedQueueConsumer) WebApplicationException(javax.ws.rs.WebApplicationException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) UriBuilder(javax.ws.rs.core.UriBuilder) POST(javax.ws.rs.POST)

Example 2 with AcknowledgedQueueConsumer

use of org.apache.activemq.artemis.rest.queue.AcknowledgedQueueConsumer in project activemq-artemis by apache.

the class SubscriptionsResource method internalHeadAcknowledgedConsumer.

private Response internalHeadAcknowledgedConsumer(UriInfo uriInfo, String consumerId) {
    AcknowledgedQueueConsumer consumer = (AcknowledgedQueueConsumer) findAcknoledgeSubscription(consumerId);
    Response.ResponseBuilder builder = Response.ok();
    headAcknowledgedConsumerResponse(uriInfo, consumer, builder);
    return builder.build();
}
Also used : Response(javax.ws.rs.core.Response) AcknowledgedQueueConsumer(org.apache.activemq.artemis.rest.queue.AcknowledgedQueueConsumer)

Aggregations

Response (javax.ws.rs.core.Response)2 AcknowledgedQueueConsumer (org.apache.activemq.artemis.rest.queue.AcknowledgedQueueConsumer)2 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 QueueConsumer (org.apache.activemq.artemis.rest.queue.QueueConsumer)1