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);
}
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));
}
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);
}
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);
}
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);
}
}
Aggregations