use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JMSFailoverTest method testAutomaticFailover.
@Test
public void testAutomaticFailover() throws Exception {
ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
jbcf.setReconnectAttempts(-1);
jbcf.setBlockOnDurableSend(true);
jbcf.setBlockOnNonDurableSend(true);
// Note we set consumer window size to a value so we can verify that consumer credit re-sending
// works properly on failover
// The value is small enough that credits will have to be resent several time
final int numMessages = 10;
final int bodySize = 1000;
jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
MyExceptionListener listener = new MyExceptionListener();
conn.setExceptionListener(listener);
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
SimpleString jmsQueueName = new SimpleString("myqueue");
coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
Queue queue = sess.createQueue("myqueue");
MessageProducer producer = sess.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
MessageConsumer consumer = sess.createConsumer(queue);
byte[] body = RandomUtil.randomBytes(bodySize);
for (int i = 0; i < numMessages; i++) {
BytesMessage bm = sess.createBytesMessage();
bm.writeBytes(body);
producer.send(bm);
}
conn.start();
JMSFailoverTest.log.info("sent messages and started connection");
Thread.sleep(2000);
JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
for (int i = 0; i < numMessages; i++) {
JMSFailoverTest.log.info("got message " + i);
BytesMessage bm = (BytesMessage) consumer.receive(1000);
Assert.assertNotNull(bm);
Assert.assertEquals(body.length, bm.getBodyLength());
}
TextMessage tm = (TextMessage) consumer.receiveNoWait();
Assert.assertNull(tm);
conn.close();
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class LargeMessageOverBridgeTest method testSendBytesAsLargeOnBridgeOnly.
/**
* This was causing a text message to ber eventually converted into large message when sent over the bridge
*
* @throws Exception
*/
@Test
public void testSendBytesAsLargeOnBridgeOnly() throws Exception {
createQueue(QUEUE);
Queue queue = (Queue) context1.lookup("queue/" + QUEUE);
Connection conn1 = cf1.createConnection();
Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer prod1 = session1.createProducer(queue);
Connection conn2 = cf2.createConnection();
Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons2 = session2.createConsumer(queue);
conn2.start();
byte[] bytes = new byte[10 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = getSamplebyte(i);
}
for (int i = 0; i < 10; i++) {
BytesMessage msg = session1.createBytesMessage();
msg.writeBytes(bytes);
prod1.send(msg);
}
session1.commit();
for (int i = 0; i < 5; i++) {
BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
assertNotNull(msg2);
msg2.acknowledge();
for (int j = 0; j < bytes.length; j++) {
assertEquals("Position " + i, msg2.readByte(), bytes[j]);
}
}
conn1.close();
conn2.close();
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class LargeMessageOverBridgeTest method testSendLargeForBridge.
/**
* The message won't be large to the client while it will be considered large through the bridge
*
* @throws Exception
*/
@Test
public void testSendLargeForBridge() throws Exception {
createQueue(QUEUE);
Queue queue = (Queue) context1.lookup("queue/" + QUEUE);
ActiveMQConnectionFactory cf1 = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, generateInVMParams(1)));
cf1.setMinLargeMessageSize(200 * 1024);
Connection conn1 = cf1.createConnection();
Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer prod1 = session1.createProducer(queue);
Connection conn2 = cf2.createConnection();
Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons2 = session2.createConsumer(queue);
conn2.start();
byte[] bytes = new byte[150 * 1024];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = getSamplebyte(i);
}
for (int i = 0; i < 10; i++) {
BytesMessage msg = session1.createBytesMessage();
msg.writeBytes(bytes);
prod1.send(msg);
}
session1.commit();
for (int i = 0; i < 5; i++) {
BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
assertNotNull(msg2);
msg2.acknowledge();
for (int j = 0; j < bytes.length; j++) {
assertEquals("Position " + i, msg2.readByte(), bytes[j]);
}
}
conn1.close();
conn2.close();
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class BodyTest method testBodyConversion.
@Test
public void testBodyConversion() throws Throwable {
try (Connection conn = cf.createConnection()) {
Session sess = conn.createSession();
MessageProducer producer = sess.createProducer(queue);
MessageConsumer cons = sess.createConsumer(queue);
conn.start();
BytesMessage bytesMessage = sess.createBytesMessage();
BytesMessage bytesMessage2 = sess.createBytesMessage();
bytesMessage2.writeInt(42);
bytesMessage2.reset();
producer.send(bytesMessage);
Message msg = cons.receiveNoWait();
producer.send(bytesMessage2);
Message msg2 = cons.receiveNoWait();
assertNotNull(msg);
assertNotNull(msg2);
// message body is empty. getBody parameter may be set to any type
Assert.assertNull(msg.getBody(java.lang.Object.class));
Assert.assertNull(msg.getBody(byte[].class));
Assert.assertNull(msg.getBody(String.class));
// message body is not empty. getBody parameter must be set to byte[].class (or java.lang.Object.class)
Assert.assertNotNull(msg2.getBody(byte[].class));
Assert.assertNotNull(msg2.getBody(java.lang.Object.class));
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JmsContextTest method testReceiveBytes.
@Test
public void testReceiveBytes() throws Exception {
JMSProducer producer = context.createProducer();
JMSConsumer consumer = context.createConsumer(queue1);
BytesMessage bytesSend = context.createBytesMessage();
bytesSend.writeByte((byte) 1);
bytesSend.writeLong(2L);
producer.send(queue1, bytesSend);
BytesMessage msgReceived = (BytesMessage) consumer.receiveNoWait();
byte[] bytesArray = msgReceived.getBody(byte[].class);
assertEquals((byte) 1, msgReceived.readByte());
assertEquals(2L, msgReceived.readLong());
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bytesArray));
assertEquals((byte) 1, dataInputStream.readByte());
assertEquals(2L, dataInputStream.readLong());
}
Aggregations