use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JmsContextTest method bytesMessage.
@Test
public void bytesMessage() throws Exception {
context = cf.createContext();
try {
JMSProducer producer = context.createProducer();
BytesMessage bMsg = context.createBytesMessage();
bMsg.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvMsgOfEachTypeCLTest");
bMsg.writeByte((byte) 1);
bMsg.writeInt(22);
CountDownLatch latch = new CountDownLatch(1);
SimpleCompletionListener listener = new SimpleCompletionListener(latch);
producer.setAsync(listener);
producer.send(queue1, bMsg);
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(listener.message.readByte(), (byte) 1);
assertEquals(listener.message.readInt(), 22);
} finally {
context.close();
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JMSLargeMessageTest method testSimpleLargeMessage2.
@Test
public void testSimpleLargeMessage2() throws Exception {
conn = cf.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = session.createProducer(queue1);
BytesMessage m = session.createBytesMessage();
m.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(10));
prod.send(m);
conn.close();
conn = cf.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = session.createConsumer(queue1);
conn.start();
BytesMessage rm = (BytesMessage) cons.receive(10000);
byte[] data = new byte[1024];
System.out.println("Message = " + rm);
int numberOfBytes = rm.readBytes(data);
Assert.assertEquals(10, numberOfBytes);
for (int j = 0; j < numberOfBytes; j++) {
Assert.assertEquals(ActiveMQTestBase.getSamplebyte(j), data[j]);
}
Assert.assertNotNull(rm);
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JMSLargeMessageTest method testWaitOnOutputStream.
@Test
public void testWaitOnOutputStream() throws Exception {
int msgSize = 1024 * 1024;
conn = cf.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = session.createProducer(queue1);
BytesMessage m = session.createBytesMessage();
m.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(msgSize));
prod.send(m);
conn.close();
conn = cf.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = session.createConsumer(queue1);
conn.start();
BytesMessage rm = (BytesMessage) cons.receive(10000);
Assert.assertNotNull(rm);
final AtomicLong numberOfBytes = new AtomicLong(0);
final AtomicInteger numberOfErrors = new AtomicInteger(0);
OutputStream out = new OutputStream() {
int position = 0;
@Override
public void write(final int b) throws IOException {
numberOfBytes.incrementAndGet();
if (ActiveMQTestBase.getSamplebyte(position++) != b) {
System.out.println("Wrong byte at position " + position);
numberOfErrors.incrementAndGet();
}
}
};
try {
rm.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(100));
Assert.fail("Exception expected!");
} catch (MessageNotWriteableException expected) {
}
rm.setObjectProperty("JMS_AMQ_SaveStream", out);
Assert.assertEquals(msgSize, numberOfBytes.get());
Assert.assertEquals(0, numberOfErrors.get());
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class PerfBase method sendMessages.
private void sendMessages(final int numberOfMessages, final int txBatchSize, final boolean durable, final boolean transacted, final boolean display, final int throttleRate, final int messageSize) throws Exception {
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(perfParams.isDurable() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
producer.setDisableMessageID(perfParams.isDisableMessageID());
producer.setDisableMessageTimestamp(perfParams.isDisableTimestamp());
BytesMessage message = session.createBytesMessage();
byte[] payload = PerfBase.randomByteArray(messageSize);
message.writeBytes(payload);
final int modulo = 2000;
TokenBucketLimiter tbl = throttleRate != -1 ? new TokenBucketLimiterImpl(throttleRate, false) : null;
boolean committed = false;
for (int i = 1; i <= numberOfMessages; i++) {
producer.send(message);
if (transacted) {
if (i % txBatchSize == 0) {
session.commit();
committed = true;
} else {
committed = false;
}
}
if (display && i % modulo == 0) {
double duration = (1.0 * System.currentTimeMillis() - start) / 1000;
PerfBase.log.info(String.format("sent %6d messages in %2.2fs", i, duration));
}
if (tbl != null) {
tbl.limit();
}
}
if (transacted && !committed) {
session.commit();
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class LargeMessageExample method main.
public static void main(final String[] args) throws Exception {
Process server = null;
Connection connection = null;
InitialContext initialContext = null;
File inputFile = null;
File outputFile = null;
boolean deleteFiles = Boolean.parseBoolean(args[1]);
try {
server = ServerUtil.startServer(args[0], LargeMessageExample.class.getSimpleName(), 0, 5000);
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the Connection Factory. This ConnectionFactory has a special attribute set on
// it.
// Messages with more than 10K are considered large
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4. Create the JMS objects
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
// Step 5. Create a huge file - this will form the body of the message we will send.
System.out.println("Creating a file to send of size " + FILE_SIZE + " bytes. This may take a little while... " + "If this is too big for your disk you can easily change the FILE_SIZE in the example.");
inputFile = new File("huge_message_to_send.dat");
createFile(inputFile, FILE_SIZE);
System.out.println("File created.");
// Step 6. Create a BytesMessage
BytesMessage message = session.createBytesMessage();
// Step 7. We set the InputStream on the message. When sending the message will read the InputStream
// until it gets EOF. In this case we point the InputStream at a file on disk, and it will suck up the entire
// file, however we could use any InputStream not just a FileInputStream.
FileInputStream fileInputStream = new FileInputStream(inputFile);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
message.setObjectProperty("JMS_AMQ_InputStream", bufferedInput);
System.out.println("Sending the huge message.");
// Step 9. Send the Message
producer.send(message);
System.out.println("Large Message sent");
System.out.println("Stopping server.");
// Step 10. To demonstrate that that we're not simply streaming the message from sending to consumer, we stop
// the server and restart it before consuming the message. This demonstrates that the large message gets
// persisted, like a
// normal persistent message, on the server. If you look at ./build/data/largeMessages you will see the
// largeMessage stored on disk the server
connection.close();
initialContext.close();
ServerUtil.killServer(server);
server = ServerUtil.startServer(args[0], "LargeMessageExample", 0, 5000);
System.out.println("Server restarted.");
// Step 11. Now the server is restarted we can recreate the JMS Objects, and start the new connection
initialContext = new InitialContext();
queue = (Queue) initialContext.lookup("queue/exampleQueue");
cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
System.out.println("Receiving message.");
// Step 12. Receive the message. When we receive the large message we initially just receive the message with
// an empty body.
BytesMessage messageReceived = (BytesMessage) messageConsumer.receive(120000);
System.out.println("Received message with: " + messageReceived.getLongProperty("_AMQ_LARGE_SIZE") + " bytes. Now streaming to file on disk.");
// Step 13. We set an OutputStream on the message. This causes the message body to be written to the
// OutputStream until there are no more bytes to be written.
// You don't have to use a FileOutputStream, you can use any OutputStream.
// You may choose to use the regular BytesMessage or
// StreamMessage interface but this method is much faster for large messages.
outputFile = new File("huge_message_received.dat");
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);
// Step 14. This will save the stream and wait until the entire message is written before continuing.
messageReceived.setObjectProperty("JMS_AMQ_SaveStream", bufferedOutput);
}
System.out.println("File streamed to disk. Size of received file on disk is " + outputFile.length());
} finally {
// Step 12. Be sure to close our resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
if (inputFile != null && deleteFiles) {
inputFile.delete();
}
if (outputFile != null && deleteFiles) {
outputFile.delete();
}
ServerUtil.killServer(server);
}
}
Aggregations