Search in sources :

Example 46 with Session

use of javax.jms.Session in project beam by apache.

the class JmsIOTest method testWriteMessage.

@Test
public void testWriteMessage() throws Exception {
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        data.add("Message " + i);
    }
    pipeline.apply(Create.of(data)).apply(JmsIO.write().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD));
    pipeline.run();
    Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
    int count = 0;
    while (consumer.receive(1000) != null) {
        count++;
    }
    assertEquals(100, count);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) Matchers.containsString(org.hamcrest.Matchers.containsString) Session(javax.jms.Session) Test(org.junit.Test)

Example 47 with Session

use of javax.jms.Session in project beam by apache.

the class JmsIOTest method testCheckpointMark.

@Test
public void testCheckpointMark() throws Exception {
    // we are using no prefetch here
    // prefetch is an ActiveMQ feature: to make efficient use of network resources the broker
    // utilizes a 'push' model to dispatch messages to consumers. However, in the case of our
    // test, it means that we can have some latency between the receiveNoWait() method used by
    // the consumer and the prefetch buffer populated by the broker. Using a prefetch to 0 means
    // that the consumer will poll for message, which is exactly what we want for the test.
    Connection connection = connectionFactoryWithoutPrefetch.createConnection(USERNAME, PASSWORD);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
    for (int i = 0; i < 10; i++) {
        producer.send(session.createTextMessage("test " + i));
    }
    producer.close();
    session.close();
    connection.close();
    JmsIO.Read spec = JmsIO.read().withConnectionFactory(connectionFactoryWithoutPrefetch).withUsername(USERNAME).withPassword(PASSWORD).withQueue(QUEUE);
    JmsIO.UnboundedJmsSource source = new JmsIO.UnboundedJmsSource(spec);
    JmsIO.UnboundedJmsReader reader = source.createReader(null, null);
    // start the reader and move to the first record
    assertTrue(reader.start());
    // consume 3 messages (NB: start already consumed the first message)
    for (int i = 0; i < 3; i++) {
        assertTrue(reader.advance());
    }
    // the messages are still pending in the queue (no ACK yet)
    assertEquals(10, count(QUEUE));
    // we finalize the checkpoint
    reader.getCheckpointMark().finalizeCheckpoint();
    // the checkpoint finalize ack the messages, and so they are not pending in the queue anymore
    assertEquals(6, count(QUEUE));
    // we read the 6 pending messages
    for (int i = 0; i < 6; i++) {
        assertTrue(reader.advance());
    }
    // still 6 pending messages as we didn't finalize the checkpoint
    assertEquals(6, count(QUEUE));
    // we finalize the checkpoint: no more message in the queue
    reader.getCheckpointMark().finalizeCheckpoint();
    assertEquals(0, count(QUEUE));
}
Also used : Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 48 with Session

use of javax.jms.Session in project beam by apache.

the class JmsIOTest method testReadMessages.

@Test
public void testReadMessages() throws Exception {
    // produce message
    Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
    TextMessage message = session.createTextMessage("This Is A Test");
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.close();
    session.close();
    connection.close();
    // read from the queue
    PCollection<JmsRecord> output = pipeline.apply(JmsIO.read().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD).withMaxNumRecords(5));
    PAssert.thatSingleton(output.apply("Count", Count.<JmsRecord>globally())).isEqualTo(new Long(5));
    pipeline.run();
    connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
    Message msg = consumer.receiveNoWait();
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 49 with Session

use of javax.jms.Session in project spring-boot by spring-projects.

the class ArtemisAutoConfigurationTests method embeddedWithPersistentMode.

@Test
public void embeddedWithPersistentMode() throws IOException, JMSException {
    File dataFolder = this.folder.newFolder();
    // Start the server and post a message to some queue
    load(EmptyConfiguration.class, "spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
    final String msgId = UUID.randomUUID().toString();
    JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
    jmsTemplate.send("TestQueue", new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(msgId);
        }
    });
    // Shutdown the broker
    this.context.close();
    // Start the server again and check if our message is still here
    load(EmptyConfiguration.class, "spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
    JmsTemplate jmsTemplate2 = this.context.getBean(JmsTemplate.class);
    jmsTemplate2.setReceiveTimeout(1000L);
    Message message = jmsTemplate2.receive("TestQueue");
    assertThat(message).isNotNull();
    assertThat(((TextMessage) message).getText()).isEqualTo(msgId);
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSException(javax.jms.JMSException) File(java.io.File) MessageCreator(org.springframework.jms.core.MessageCreator) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 50 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class AbstractMessageListenerContainer method doInvokeListener.

/**
	 * Invoke the specified listener as Spring SessionAwareMessageListener,
	 * exposing a new JMS Session (potentially with its own transaction)
	 * to the listener if demanded.
	 * @param listener the Spring SessionAwareMessageListener to invoke
	 * @param session the JMS Session to operate on
	 * @param message the received JMS Message
	 * @throws JMSException if thrown by JMS API methods
	 * @see SessionAwareMessageListener
	 * @see #setExposeListenerSession
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message) throws JMSException {
    Connection conToClose = null;
    Session sessionToClose = null;
    try {
        Session sessionToUse = session;
        if (!isExposeListenerSession()) {
            // We need to expose a separate Session.
            conToClose = createConnection();
            sessionToClose = createSession(conToClose);
            sessionToUse = sessionToClose;
        }
        // Actually invoke the message listener...
        listener.onMessage(message, sessionToUse);
        // Clean up specially exposed Session, if any.
        if (sessionToUse != session) {
            if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
                // Transacted session created by this container -> commit.
                JmsUtils.commitIfNecessary(sessionToUse);
            }
        }
    } finally {
        JmsUtils.closeSession(sessionToClose);
        JmsUtils.closeConnection(conToClose);
    }
}
Also used : Connection(javax.jms.Connection) Session(javax.jms.Session)

Aggregations

Session (javax.jms.Session)281 MessageProducer (javax.jms.MessageProducer)131 Connection (javax.jms.Connection)120 Test (org.junit.Test)110 Message (javax.jms.Message)86 TextMessage (javax.jms.TextMessage)84 JMSException (javax.jms.JMSException)80 MessageConsumer (javax.jms.MessageConsumer)64 Destination (javax.jms.Destination)46 Topic (javax.jms.Topic)40 ConnectionFactory (javax.jms.ConnectionFactory)37 ObjectMessage (javax.jms.ObjectMessage)28 Queue (javax.jms.Queue)26 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MessageCreator (org.springframework.jms.core.MessageCreator)15 BytesMessage (javax.jms.BytesMessage)14 MapMessage (javax.jms.MapMessage)12 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)12