use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JMSReconnectTest method testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue.
// Test that non durable JMS sub gets recreated in auto reconnect
private void testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue(final boolean nonDurableSub) throws Exception {
ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
jbcf.setReconnectAttempts(-1);
Connection conn = jbcf.createConnection();
MyExceptionListener listener = new MyExceptionListener();
conn.setExceptionListener(listener);
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
Destination dest;
if (nonDurableSub) {
coreSession.createQueue("mytopic", "blahblah", null, false);
dest = ActiveMQJMSClient.createTopic("mytopic");
} else {
dest = sess.createTemporaryQueue();
}
MessageProducer producer = sess.createProducer(dest);
// Create a non durable subscriber
MessageConsumer consumer = sess.createConsumer(dest);
this.server.stop();
this.server.start();
// Allow client some time to reconnect
Thread.sleep(3000);
final int numMessages = 100;
byte[] body = RandomUtil.randomBytes(1000);
for (int i = 0; i < numMessages; i++) {
BytesMessage bm = sess.createBytesMessage();
bm.writeBytes(body);
producer.send(bm);
}
conn.start();
for (int i = 0; i < numMessages; 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();
Assert.assertNotNull(listener.e);
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class ReSendMessageTest method testResendWithLargeMessage.
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testResendWithLargeMessage() throws Exception {
conn = cf.createConnection();
conn.start();
Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
ArrayList<Message> msgs = new ArrayList<>();
for (int i = 0; i < 10; i++) {
BytesMessage bm = sess.createBytesMessage();
bm.setObjectProperty(ActiveMQJMSConstants.JMS_ACTIVEMQ_INPUT_STREAM, ActiveMQTestBase.createFakeLargeStream(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE));
msgs.add(bm);
MapMessage mm = sess.createMapMessage();
mm.setBoolean("boolean", true);
mm.setByte("byte", (byte) 3);
mm.setBytes("bytes", new byte[] { (byte) 3, (byte) 4, (byte) 5 });
mm.setChar("char", (char) 6);
mm.setDouble("double", 7.0);
mm.setFloat("float", 8.0f);
mm.setInt("int", 9);
mm.setLong("long", 10L);
mm.setObject("object", new String("this is an object"));
mm.setShort("short", (short) 11);
mm.setString("string", "this is a string");
msgs.add(mm);
msgs.add(sess.createTextMessage("hello" + i));
msgs.add(sess.createObjectMessage(new SomeSerializable("hello" + i)));
}
internalTestResend(msgs, sess);
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class MessageCompressionTest method sendTestBytesMessage.
private void sendTestBytesMessage(ActiveMQConnectionFactory factory, String message) throws JMSException, UnsupportedEncodingException {
ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(message.getBytes(StandardCharsets.UTF_8));
producer.send(bytesMessage);
connection.close();
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class MQTTTest method doTestJmsMapping.
// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
public void doTestJmsMapping(String destinationName) throws Exception {
// start up jms consumer
Connection jmsConn = cf.createConnection();
Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(dest);
jmsConn.start();
// set up mqtt producer
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo3");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
int messagesToSend = 5;
// publish
for (int i = 0; i < messagesToSend; ++i) {
connection.publish("test/foo", "hello world".getBytes(), QoS.AT_LEAST_ONCE, false);
}
connection.disconnect();
for (int i = 0; i < messagesToSend; i++) {
javax.jms.Message message = consumer.receive(2 * 1000);
assertNotNull(message);
assertTrue(message instanceof BytesMessage);
BytesMessage bytesMessage = (BytesMessage) message;
int length = (int) bytesMessage.getBodyLength();
byte[] buffer = new byte[length];
bytesMessage.readBytes(buffer);
assertEquals("hello world", new String(buffer));
}
jmsConn.close();
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class MQTTTest method doTestSendMQTTReceiveJMS.
public void doTestSendMQTTReceiveJMS(String jmsTopicAddress, String mqttAddress) throws Exception {
final MQTTClientProvider provider = getMQTTClientProvider();
initializeConnection(provider);
// send retained message
final String address = mqttAddress;
final String RETAINED = "RETAINED";
final byte[] payload = RETAINED.getBytes();
Connection connection = cf.createConnection();
// MUST set to true to receive retained messages
connection.start();
Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
javax.jms.Queue jmsQueue = s.createQueue(jmsTopicAddress);
MessageConsumer consumer = s.createConsumer(jmsQueue);
provider.publish(address, RETAINED.getBytes(), AT_LEAST_ONCE, true);
// check whether we received retained message on JMS subscribe
BytesMessage message = (BytesMessage) consumer.receive(5000);
assertNotNull("Should get retained message", message);
byte[] b = new byte[8];
message.readBytes(b);
assertArrayEquals(payload, b);
for (int i = 0; i < NUM_MESSAGES; i++) {
String p = "Test Message: " + i;
provider.publish(address, p.getBytes(), AT_LEAST_ONCE);
message = (BytesMessage) consumer.receive(5000);
assertNotNull("Should get a message", message);
byte[] bytePayload = new byte[p.getBytes().length];
message.readBytes(bytePayload);
assertArrayEquals(payload, b);
}
connection.close();
provider.disconnect();
}
Aggregations