// Create connection
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
// Create session
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Create sender
QueueSender queueSender = queueSession.createSender(queue);
// Create sender for context MDB
QueueSender queueSenderContext = queueSession.createSender(queueContext);
// Send messages
TextMessage message = null;
for (int i = 0; i < NUMBER_MESSAGES; i++) {
message = queueSession.createTextMessage();
String text = "Message_" + i;
message.setText(text);
queueSender.send(message);
System.out.println("Message [" + message.getJMSMessageID() + ", text:" + text + "] sent");
}
message = queueSession.createTextMessage();
message.setText("Message that should be accepted after several attempts");
queueSenderContext.send(message);
// Close JMS objects
queueSender.close();
queueSenderContext.close();
queueSession.close();
queueConnection.close();
}