Search in sources :

Example 11 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class XmlDataImporter method processMessageBody.

private File processMessageBody(final ICoreMessage message) throws XMLStreamException, IOException {
    File tempFileName = null;
    boolean isLarge = false;
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attributeName = reader.getAttributeLocalName(i);
        if (XmlDataConstants.MESSAGE_IS_LARGE.equals(attributeName)) {
            isLarge = Boolean.parseBoolean(reader.getAttributeValue(i));
        }
    }
    reader.next();
    if (logger.isDebugEnabled()) {
        logger.debug("XMLStreamReader impl: " + reader);
    }
    if (isLarge) {
        tempFileName = File.createTempFile("largeMessage", ".tmp");
        if (logger.isDebugEnabled()) {
            logger.debug("Creating temp file " + tempFileName + " for large message.");
        }
        try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFileName))) {
            getMessageBodyBytes(new MessageBodyBytesProcessor() {

                @Override
                public void processBodyBytes(byte[] bytes) throws IOException {
                    out.write(bytes);
                }
            });
        }
        FileInputStream fileInputStream = new FileInputStream(tempFileName);
        BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
        ((ClientMessage) message).setBodyInputStream(bufferedInput);
    } else {
        getMessageBodyBytes(new MessageBodyBytesProcessor() {

            @Override
            public void processBodyBytes(byte[] bytes) throws IOException {
                message.getBodyBuffer().writeBytes(bytes);
            }
        });
    }
    return tempFileName;
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) IOException(java.io.IOException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 12 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class PostMessage method createActiveMQMessage.

protected ClientMessage createActiveMQMessage(HttpHeaders headers, byte[] body, boolean durable, Long ttl, Long expiration, Integer priority, ClientSession session) throws Exception {
    ClientMessage message = session.createMessage(Message.BYTES_TYPE, durable);
    // HORNETQ-962
    UUID uid = UUIDGenerator.getInstance().generateUUID();
    message.setUserID(uid);
    if (expiration != null) {
        message.setExpiration(expiration.longValue());
    } else if (ttl != null) {
        message.setExpiration(System.currentTimeMillis() + ttl.longValue());
    } else if (producerTimeToLive > 0) {
        message.setExpiration(System.currentTimeMillis() + producerTimeToLive);
    }
    if (priority != null) {
        byte p = priority.byteValue();
        if (p >= 0 && p <= 9) {
            message.setPriority(p);
        }
    }
    HttpMessageHelper.writeHttpMessage(headers, body, message);
    return message;
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) UUID(org.apache.activemq.artemis.utils.UUID)

Example 13 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class PostMessage method publish.

public void publish(HttpHeaders headers, byte[] body, String dup, boolean durable, Long ttl, Long expiration, Integer priority) throws Exception {
    Pooled pooled = getPooled();
    try {
        ClientProducer producer = pooled.producer;
        ClientMessage message = createActiveMQMessage(headers, body, durable, ttl, expiration, priority, pooled.session);
        message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), dup);
        producer.send(message);
        ActiveMQRestLogger.LOGGER.debug("Sent message: " + message);
        pool.add(pooled);
    } catch (Exception ex) {
        try {
            pooled.session.close();
        } catch (ActiveMQException e) {
        }
        addPooled();
        throw ex;
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 14 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class QueueConsumer method pollWithIndex.

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;
    }
}
Also used : Response(javax.ws.rs.core.Response) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 15 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class QueueConsumer method receiveFromConsumer.

protected ClientMessage receiveFromConsumer(long timeoutSecs) throws Exception {
    ClientMessage m = null;
    if (timeoutSecs <= 0) {
        m = consumer.receive(1);
    } else {
        m = consumer.receive(timeoutSecs * 1000);
    }
    ActiveMQRestLogger.LOGGER.debug("Returning message " + m + " from consumer: " + consumer);
    return m;
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)889 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)708 Test (org.junit.Test)647 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)623 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)606 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)445 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)363 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)167 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)140 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)102 CountDownLatch (java.util.concurrent.CountDownLatch)99 Configuration (org.apache.activemq.artemis.core.config.Configuration)87 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)85 Queue (org.apache.activemq.artemis.core.server.Queue)72 Xid (javax.transaction.xa.Xid)67 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)54 HashMap (java.util.HashMap)53 ArrayList (java.util.ArrayList)51 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)47