2014-12-11 07:17:29 -05:00
|
|
|
# Large Messages
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-27 17:32:30 -04:00
|
|
|
Apache ActiveMQ Artemis supports sending and receiving of huge messages, even when the
|
2014-12-04 10:25:29 -05:00
|
|
|
client and server are running with limited memory. The only realistic
|
|
|
|
limit to the size of a message that can be sent or consumed is the
|
|
|
|
amount of disk space you have available. We have tested sending and
|
|
|
|
consuming messages up to 8 GiB in size with a client and server running
|
|
|
|
in just 50MiB of RAM!
|
|
|
|
|
|
|
|
To send a large message, the user can set an `InputStream` on a message
|
2015-04-27 17:32:30 -04:00
|
|
|
body, and when that message is sent, Apache ActiveMQ Artemis will read the
|
2014-12-04 10:25:29 -05:00
|
|
|
`InputStream`. A `FileInputStream` could be used for example to send a
|
|
|
|
huge message from a huge file on disk.
|
|
|
|
|
|
|
|
As the `InputStream` is read the data is sent to the server as a stream
|
|
|
|
of fragments. The server persists these fragments to disk as it receives
|
|
|
|
them and when the time comes to deliver them to a consumer they are read
|
|
|
|
back of the disk, also in fragments and sent down the wire. When the
|
|
|
|
consumer receives a large message it initially receives just the message
|
|
|
|
with an empty body, it can then set an `OutputStream` on the message to
|
|
|
|
stream the huge message body to a file on disk or elsewhere. At no time
|
|
|
|
is the entire message body stored fully in memory, either on the client
|
|
|
|
or the server.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Configuring the server
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Large messages are stored on a disk directory on the server side, as
|
|
|
|
configured on the main configuration file.
|
|
|
|
|
|
|
|
The configuration property `large-messages-directory` specifies where
|
2017-03-09 11:59:09 -05:00
|
|
|
large messages are stored. For JDBC persistence the `large-message-table`
|
|
|
|
should be configured.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
<configuration xmlns="urn:activemq"
|
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
2015-04-29 05:49:43 -04:00
|
|
|
xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
|
2014-12-04 10:25:29 -05:00
|
|
|
...
|
|
|
|
<large-messages-directory>/data/large-messages</large-messages-directory>
|
|
|
|
...
|
|
|
|
</configuration
|
|
|
|
|
2017-03-09 11:59:09 -05:00
|
|
|
By default the large message directory is `data/largemessages` and `large-message-table` is
|
|
|
|
configured as "LARGE_MESSAGE_TABLE".
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2017-03-09 11:59:09 -05:00
|
|
|
For the best performance we recommend using file store with large messages directory stored
|
|
|
|
on a different physical volume to the message journal or paging directory.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Configuring Parameters
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Any message larger than a certain size is considered a large message.
|
|
|
|
Large messages will be split up and sent in fragments. This is
|
2015-02-25 08:37:19 -05:00
|
|
|
determined by the parameter `minLargeMessageSize`
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
> **Note**
|
|
|
|
>
|
2015-04-27 17:32:30 -04:00
|
|
|
> Apache ActiveMQ Artemis messages are encoded using 2 bytes per character so if the
|
2014-12-04 10:25:29 -05:00
|
|
|
> message data is filled with ASCII characters (which are 1 byte) the
|
2015-04-27 17:32:30 -04:00
|
|
|
> size of the resulting Apache ActiveMQ Artemis message would roughly double. This is
|
2014-12-04 10:25:29 -05:00
|
|
|
> important when calculating the size of a "large" message as it may
|
2015-02-25 08:37:19 -05:00
|
|
|
> appear to be less than the `minLargeMessageSize` before it is sent,
|
2014-12-04 10:25:29 -05:00
|
|
|
> but it then turns into a "large" message once it is encoded.
|
|
|
|
|
|
|
|
The default value is 100KiB.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
### Using Core API
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-27 17:32:30 -04:00
|
|
|
If the Apache ActiveMQ Artemis Core API is used, the minimal large message size is
|
2014-12-04 10:25:29 -05:00
|
|
|
specified by `ServerLocator.setMinLargeMessageSize`.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
ServerLocator locator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()))
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
locator.setMinLargeMessageSize(25 * 1024);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
ClientSessionFactory factory = ActiveMQClient.createClientSessionFactory();
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
[Configuring the transport directly from the client side](configuring-transports.md) will provide more information on how to instantiate the session
|
2014-12-04 10:25:29 -05:00
|
|
|
factory.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
### Using JMS
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
If JNDI is used to instantiate and look up the connection factory, the
|
|
|
|
minimum large message size is configured in the JNDI context
|
|
|
|
environment, e.g. `jndi.properties`. Here's a simple example using the
|
|
|
|
"ConnectionFactory" connection factory which is available in the context
|
|
|
|
by default:
|
|
|
|
|
2015-04-30 05:44:16 -04:00
|
|
|
java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
|
2015-02-25 08:37:19 -05:00
|
|
|
connectionFactory.myConnectionFactory=tcp://localhost:61616?minLargeMessageSize=250000
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
If the connection factory is being instantiated directly, the minimum
|
|
|
|
large message size is specified by
|
|
|
|
`ActiveMQConnectionFactory.setMinLargeMessageSize`.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
### Compressed Large Messages
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
You can choose to send large messages in compressed form using `
|
|
|
|
compress-large-messages` attributes.
|
|
|
|
|
2015-02-25 08:37:19 -05:00
|
|
|
#### `compressLargeMessages`
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-02-25 08:37:19 -05:00
|
|
|
If you specify the boolean property `compressLargeMessages` on the
|
2014-12-04 10:25:29 -05:00
|
|
|
`server locator` or `ConnectionFactory` as true, The system will use the
|
|
|
|
ZIP algorithm to compress the message body as the message is transferred
|
|
|
|
to the server's side. Notice that there's no special treatment at the
|
|
|
|
server's side, all the compressing and uncompressing is done at the
|
|
|
|
client.
|
|
|
|
|
|
|
|
If the compressed size of a large message is below `
|
2015-02-25 08:37:19 -05:00
|
|
|
minLargeMessageSize`, it is sent to server as regular
|
2014-12-04 10:25:29 -05:00
|
|
|
messages. This means that the message won't be written into the server's
|
|
|
|
large-message data directory, thus reducing the disk I/O.
|
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
###
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
If JNDI is used to instantiate and look up the connection factory, large
|
|
|
|
message compression can be configured in the JNDI context environment,
|
|
|
|
e.g. `jndi.properties`. Here's a simple example using the
|
|
|
|
"ConnectionFactory" connection factory which is available in the context
|
|
|
|
by default:
|
|
|
|
|
2015-04-30 05:44:16 -04:00
|
|
|
java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
|
2015-02-25 08:37:19 -05:00
|
|
|
connectionFactory.myConnectionFactory=tcp://localhost:61616?compressLargeMessages=true
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Streaming large messages
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-27 17:32:30 -04:00
|
|
|
Apache ActiveMQ Artemis supports setting the body of messages using input and output
|
2014-12-04 10:25:29 -05:00
|
|
|
streams (`java.lang.io`)
|
|
|
|
|
|
|
|
These streams are then used directly for sending (input streams) and
|
|
|
|
receiving (output streams) messages.
|
|
|
|
|
|
|
|
When receiving messages there are 2 ways to deal with the output stream;
|
|
|
|
you may choose to block while the output stream is recovered using the
|
|
|
|
method `ClientMessage.saveOutputStream` or alternatively using the
|
|
|
|
method `ClientMessage.setOutputstream` which will asynchronously write
|
|
|
|
the message to the stream. If you choose the latter the consumer must be
|
|
|
|
kept alive until the message has been fully received.
|
|
|
|
|
|
|
|
You can use any kind of stream you like. The most common use case is to
|
|
|
|
send files stored in your disk, but you could also send things like JDBC
|
|
|
|
Blobs, `SocketInputStream`, things you recovered from `HTTPRequests`
|
|
|
|
etc. Anything as long as it implements `java.io.InputStream` for sending
|
|
|
|
messages or `java.io.OutputStream` for receiving them.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
### Streaming over Core API
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
The following table shows a list of methods available at `ClientMessage`
|
|
|
|
which are also available through JMS by the use of object properties.
|
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
<table summary="Server Configuration" border="1">
|
|
|
|
<colgroup>
|
|
|
|
<col/>
|
|
|
|
<col/>
|
|
|
|
<col/>
|
|
|
|
<col/>
|
|
|
|
</colgroup>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Description</th>
|
|
|
|
<th>JMS Equivalent</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>setBodyInputStream(InputStream)</td>
|
|
|
|
<td>Set the InputStream used to read a message body when sending it.</td>
|
2015-04-14 12:07:26 -04:00
|
|
|
<td>JMS_AMQ_InputStream</td>
|
2015-01-23 09:28:07 -05:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>setOutputStream(OutputStream)</td>
|
|
|
|
<td>Set the OutputStream that will receive the body of a message. This method does not block.</td>
|
2015-04-14 12:07:26 -04:00
|
|
|
<td>JMS_AMQ_OutputStream</td>
|
2015-01-23 09:28:07 -05:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>saveOutputStream(OutputStream)</td>
|
|
|
|
<td>Save the body of the message to the `OutputStream`. It will block until the entire content is transferred to the `OutputStream`.</td>
|
2015-04-14 12:07:26 -04:00
|
|
|
<td>JMS_AMQ_SaveStream</td>
|
2015-01-23 09:28:07 -05:00
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-29 13:20:31 -04:00
|
|
|
: org.apache.activemq.artemis.api.core.client.ClientMessage API
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
To set the output stream when receiving a core message:
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
ClientMessage msg = consumer.receive(...);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
// This will block here until the stream was transferred
|
2015-01-23 09:28:07 -05:00
|
|
|
msg.saveOutputStream(someOutputStream);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
ClientMessage msg2 = consumer.receive(...);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
// This will not wait the transfer to finish
|
2017-02-27 13:15:47 -05:00
|
|
|
msg2.setOutputStream(someOtherOutputStream);
|
2014-12-11 07:17:29 -05:00
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Set the input stream when sending a core message:
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
ClientMessage msg = session.createMessage();
|
|
|
|
msg.setInputStream(dataInputStream);
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Notice also that for messages with more than 2GiB the getBodySize() will
|
|
|
|
return invalid values since this is an integer (which is also exposed to
|
|
|
|
the JMS API). On those cases you can use the message property
|
2015-04-14 12:07:26 -04:00
|
|
|
_AMQ_LARGE_SIZE.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
### Streaming over JMS
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-27 17:32:30 -04:00
|
|
|
When using JMS, Apache ActiveMQ Artemis maps the streaming methods on the core API (see
|
2014-12-11 07:17:29 -05:00
|
|
|
ClientMessage API table above) by setting object properties . You can use the method
|
2014-12-04 10:25:29 -05:00
|
|
|
`Message.setObjectProperty` to set the input and output streams.
|
|
|
|
|
|
|
|
The `InputStream` can be defined through the JMS Object Property
|
2015-04-14 12:07:26 -04:00
|
|
|
JMS_AMQ_InputStream on messages being sent:
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
BytesMessage message = session.createBytesMessage();
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
FileInputStream fileInputStream = new FileInputStream(fileInput);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-14 12:07:26 -04:00
|
|
|
message.setObjectProperty("JMS_AMQ_InputStream", bufferedInput);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
someProducer.send(message);
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
The `OutputStream` can be set through the JMS Object Property
|
2015-04-14 12:07:26 -04:00
|
|
|
JMS_AMQ_SaveStream on messages being received in a blocking way.
|
2014-12-11 07:17:29 -05:00
|
|
|
|
|
|
|
``` java
|
|
|
|
BytesMessage messageReceived = (BytesMessage)messageConsumer.receive(120000);
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
File outputFile = new File("huge_message_received.dat");
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
// This will block until the entire content is saved on disk
|
2015-04-14 12:07:26 -04:00
|
|
|
messageReceived.setObjectProperty("JMS_AMQ_SaveStream", bufferedOutput);
|
2014-12-11 07:17:29 -05:00
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Setting the `OutputStream` could also be done in a non blocking way
|
2015-04-14 12:07:26 -04:00
|
|
|
using the property JMS_AMQ_OutputStream.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
// This won't wait the stream to finish. You need to keep the consumer active.
|
2015-04-14 12:07:26 -04:00
|
|
|
messageReceived.setObjectProperty("JMS_AMQ_OutputStream", bufferedOutput);
|
2014-12-11 07:17:29 -05:00
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
> **Note**
|
|
|
|
>
|
|
|
|
> When using JMS, Streaming large messages are only supported on
|
|
|
|
> `StreamMessage` and `BytesMessage`.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Streaming Alternative
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
If you choose not to use the `InputStream` or `OutputStream` capability
|
2015-04-27 17:32:30 -04:00
|
|
|
of Apache ActiveMQ Artemis You could still access the data directly in an alternative
|
2014-12-04 10:25:29 -05:00
|
|
|
fashion.
|
|
|
|
|
|
|
|
On the Core API just get the bytes of the body as you normally would.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
ClientMessage msg = consumer.receive();
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
byte[] bytes = new byte[1024];
|
|
|
|
for (int i = 0 ; i < msg.getBodySize(); i += bytes.length)
|
|
|
|
{
|
|
|
|
msg.getBody().readBytes(bytes);
|
|
|
|
// Whatever you want to do with the bytes
|
|
|
|
}
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
If using JMS API, `BytesMessage` and `StreamMessage` also supports it
|
|
|
|
transparently.
|
2014-12-11 07:17:29 -05:00
|
|
|
``` java
|
|
|
|
BytesMessage rm = (BytesMessage)cons.receive(10000);
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
byte data[] = new byte[1024];
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
for (int i = 0; i < rm.getBodyLength(); i += 1024)
|
|
|
|
{
|
|
|
|
int numberOfBytes = rm.readBytes(data);
|
|
|
|
// Do whatever you want with the data
|
2015-01-23 09:28:07 -05:00
|
|
|
}
|
2014-12-11 07:17:29 -05:00
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Large message example
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
Please see the [examples](examples.md) chapter for an example which shows how large message is configured
|
2014-12-04 10:25:29 -05:00
|
|
|
and used with JMS.
|