Search in sources :

Example 71 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class MessageListenerExample method main.

public static void main(final String[] args) throws Exception {
    Connection connection = null;
    try {
        ConnectionFactory cf = new ActiveMQConnectionFactory();
        connection = cf.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("exampleQueue");
        int nMessages = 1000;
        CountDownLatch latch = new CountDownLatch(nMessages);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        messageConsumer.setMessageListener(new LocalListener(latch));
        connection.start();
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < 1000; i++) {
            TextMessage message = session.createTextMessage("This is a text message " + i);
            System.out.println("Sent message: " + message.getText());
            producer.send(message);
        }
        if (!latch.await(5, TimeUnit.SECONDS)) {
            throw new RuntimeException("listener didn't receive all the messages");
        }
        System.out.println("Finished ok!");
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 72 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class NetworkRestartTest method doSetUp.

protected void doSetUp() throws Exception {
    remoteBroker = createRemoteBroker();
    remoteBroker.start();
    remoteBroker.waitUntilStarted();
    localBroker = createLocalBroker();
    localBroker.start();
    localBroker.waitUntilStarted();
    String localURI = "tcp://localhost:61616";
    String remoteURI = "tcp://localhost:61617";
    ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(localURI);
    localConnection = fac.createConnection();
    localConnection.setClientID("local");
    localConnection.start();
    fac = new ActiveMQConnectionFactory(remoteURI);
    fac.setWatchTopicAdvisories(false);
    remoteConnection = fac.createConnection();
    remoteConnection.setClientID("remote");
    remoteConnection.start();
    localSession = localConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    remoteSession = remoteConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory)

Example 73 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class FailoverStaticNetworkTest method createConnectionFactory.

protected ConnectionFactory createConnectionFactory(final BrokerService broker) throws Exception {
    String url = broker.getTransportConnectors().get(0).getServer().getConnectURI().toString();
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
    connectionFactory.setOptimizedMessageDispatch(true);
    connectionFactory.setDispatchAsync(false);
    connectionFactory.setUseAsyncSend(false);
    connectionFactory.setOptimizeAcknowledge(false);
    connectionFactory.setAlwaysSyncSend(true);
    return connectionFactory;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory)

Example 74 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class MessageCompressionTest method testStreamMessageCompression.

public void testStreamMessageCompression() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
    factory.setUseCompression(true);
    sendTestStreamMessage(factory, TEXT);
    ActiveMQStreamMessage streamMessage = receiveTestStreamMessage(factory);
    int compressedSize = streamMessage.getContent().getLength();
    assertTrue(streamMessage.isCompressed());
    boolean booleanVal = streamMessage.readBoolean();
    assertTrue(booleanVal);
    byte byteVal = streamMessage.readByte();
    assertEquals((byte) 10, byteVal);
    byte[] originVal = TEXT.getBytes();
    byte[] bytesVal = new byte[originVal.length];
    streamMessage.readBytes(bytesVal);
    for (int i = 0; i < bytesVal.length; i++) {
        assertTrue(bytesVal[i] == originVal[i]);
    }
    char charVal = streamMessage.readChar();
    assertEquals('A', charVal);
    double doubleVal = streamMessage.readDouble();
    assertEquals(55.3D, doubleVal, 0.1D);
    float floatVal = streamMessage.readFloat();
    assertEquals(79.1F, floatVal, 0.1F);
    int intVal = streamMessage.readInt();
    assertEquals(37, intVal);
    long longVal = streamMessage.readLong();
    assertEquals(56652L, longVal);
    Object objectVal = streamMessage.readObject();
    Object origVal = new String("VVVV");
    assertTrue(objectVal.equals(origVal));
    short shortVal = streamMessage.readShort();
    assertEquals((short) 333, shortVal);
    String strVal = streamMessage.readString();
    assertEquals(TEXT, strVal);
    factory = new ActiveMQConnectionFactory(connectionUri);
    factory.setUseCompression(false);
    sendTestStreamMessage(factory, TEXT);
    streamMessage = receiveTestStreamMessage(factory);
    int unCompressedSize = streamMessage.getContent().getLength();
    System.out.println("compressedSize: " + compressedSize + " un: " + unCompressedSize);
    assertTrue("expected: compressed Size '" + compressedSize + "' < unCompressedSize '" + unCompressedSize + "'", compressedSize < unCompressedSize);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory)

Example 75 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class MessageCompressionTest method testTextMessageCompression.

public void testTextMessageCompression() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
    factory.setUseCompression(true);
    sendTestMessage(factory, TEXT);
    ActiveMQTextMessage message = receiveTestMessage(factory);
    int compressedSize = message.getContent().getLength();
    factory = new ActiveMQConnectionFactory(connectionUri);
    factory.setUseCompression(false);
    sendTestMessage(factory, TEXT);
    message = receiveTestMessage(factory);
    int unCompressedSize = message.getContent().getLength();
    assertEquals(TEXT, message.getText());
    assertTrue("expected: compressed Size '" + compressedSize + "' < unCompressedSize '" + unCompressedSize + "'", compressedSize < unCompressedSize);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)436 Session (javax.jms.Session)148 Connection (javax.jms.Connection)144 Test (org.junit.Test)107 MessageConsumer (javax.jms.MessageConsumer)103 TextMessage (javax.jms.TextMessage)81 MessageProducer (javax.jms.MessageProducer)79 Message (javax.jms.Message)74 Queue (javax.jms.Queue)66 JMSException (javax.jms.JMSException)65 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)51 ConnectionFactory (javax.jms.ConnectionFactory)48 Destination (javax.jms.Destination)42 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)36 BrokerService (org.apache.activemq.broker.BrokerService)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 Before (org.junit.Before)27 MessageListener (javax.jms.MessageListener)26 CamelContext (org.apache.camel.CamelContext)26 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)23