|
ActiveMQ example source code file (JMSMessageTest.java)
The ActiveMQ JMSMessageTest.java source code/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.activemq; import java.net.URISyntaxException; import java.util.Enumeration; import java.util.HashMap; import java.util.Vector; import javax.jms.BytesMessage; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageEOFException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import junit.framework.Test; import org.apache.activemq.command.ActiveMQDestination; /** * Test cases used to test the JMS message consumer. * * */ public class JMSMessageTest extends JmsTestSupport { public ActiveMQDestination destination; public int deliveryMode = DeliveryMode.NON_PERSISTENT; public int prefetch; public int ackMode; public byte destinationType = ActiveMQDestination.QUEUE_TYPE; public boolean durableConsumer; public String connectURL = "vm://localhost?marshal=false"; /** * Run all these tests in both marshaling and non-marshaling mode. */ public void initCombos() { addCombinationValues("connectURL", new Object[] {"vm://localhost?marshal=false", "vm://localhost?marshal=true"}); addCombinationValues("deliveryMode", new Object[] {Integer.valueOf(DeliveryMode.NON_PERSISTENT), Integer.valueOf(DeliveryMode.PERSISTENT)}); addCombinationValues("destinationType", new Object[] {Byte.valueOf(ActiveMQDestination.QUEUE_TYPE)}); } public void testTextMessage() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // Send the message. { TextMessage message = session.createTextMessage(); message.setText("Hi"); producer.send(message); } // Check the Message { TextMessage message = (TextMessage)consumer.receive(1000); assertNotNull(message); assertEquals("Hi", message.getText()); } assertNull(consumer.receiveNoWait()); } public static Test suite() { return suite(JMSMessageTest.class); } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } protected ConnectionFactory createConnectionFactory() throws URISyntaxException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectURL); return factory; } public void testBytesMessageLength() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // Send the message { BytesMessage message = session.createBytesMessage(); message.writeInt(1); message.writeInt(2); message.writeInt(3); message.writeInt(4); producer.send(message); } // Check the message. { BytesMessage message = (BytesMessage)consumer.receive(1000); assertNotNull(message); assertEquals(16, message.getBodyLength()); } assertNull(consumer.receiveNoWait()); } public void testObjectMessage() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // send the message. { ObjectMessage message = session.createObjectMessage(); message.setObject("Hi"); producer.send(message); } // Check the message { ObjectMessage message = (ObjectMessage)consumer.receive(1000); assertNotNull(message); assertEquals("Hi", message.getObject()); } assertNull(consumer.receiveNoWait()); } public void testBytesMessage() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // Send the message { BytesMessage message = session.createBytesMessage(); message.writeBoolean(true); producer.send(message); } // Check the message { BytesMessage message = (BytesMessage)consumer.receive(1000); assertNotNull(message); assertTrue(message.readBoolean()); try { message.readByte(); fail("Expected exception not thrown."); } catch (MessageEOFException e) { } } assertNull(consumer.receiveNoWait()); } public void testStreamMessage() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // Send the message. { StreamMessage message = session.createStreamMessage(); message.writeString("This is a test to see how it works."); producer.send(message); } // Check the message. { StreamMessage message = (StreamMessage)consumer.receive(1000); assertNotNull(message); // Invalid conversion should throw exception and not move the stream // position. try { message.readByte(); fail("Should have received NumberFormatException"); } catch (NumberFormatException e) { } assertEquals("This is a test to see how it works.", message.readString()); // Invalid conversion should throw exception and not move the stream // position. try { message.readByte(); fail("Should have received MessageEOFException"); } catch (MessageEOFException e) { } } assertNull(consumer.receiveNoWait()); } public void testMapMessage() throws Exception { // Receive a message with the JMS API connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); MessageProducer producer = session.createProducer(destination); // send the message. { MapMessage message = session.createMapMessage(); message.setBoolean("boolKey", true); producer.send(message); } // get the message. { MapMessage message = (MapMessage)consumer.receive(1000); assertNotNull(message); assertTrue(message.getBoolean("boolKey")); } assertNull(consumer.receiveNoWait()); } static class ForeignMessage implements TextMessage { public int deliveryMode; private String messageId; private long timestamp; private String correlationId; private Destination replyTo; private Destination destination; private boolean redelivered; private String type; private long expiration; private int priority; private String text; private HashMap<String, Object> props = new HashMap Other ActiveMQ examples (source code examples)Here is a short list of links related to this ActiveMQ JMSMessageTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.