use of javax.jms.TemporaryQueue in project activemq-artemis by apache.
the class TemporaryQueueExample method main.
public static void main(final String[] args) throws Exception {
Connection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Look-up the JMS connection factory
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 3. Create a JMS Connection
connection = cf.createConnection();
// Step 4. Start the connection
connection.start();
// Step 5. Create a JMS session with AUTO_ACKNOWLEDGE mode
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a Temporary Queue
TemporaryQueue tempQueue = session.createTemporaryQueue();
System.out.println("Temporary queue is created: " + tempQueue);
// Step 7. Create a JMS message producer
MessageProducer messageProducer = session.createProducer(tempQueue);
// Step 8. Create a text message
TextMessage message = session.createTextMessage("This is a text message");
// Step 9. Send the text message to the queue
messageProducer.send(message);
System.out.println("Sent message: " + message.getText());
// Step 11. Create a message consumer
MessageConsumer messageConsumer = session.createConsumer(tempQueue);
// Step 12. Receive the message from the queue
message = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + message.getText());
// Step 13. Close the consumer and producer
messageConsumer.close();
messageProducer.close();
// Step 14. Delete the temporary queue
tempQueue.delete();
// Step 15. Create another temporary queue.
TemporaryQueue tempQueue2 = session.createTemporaryQueue();
System.out.println("Another temporary queue is created: " + tempQueue2);
// Step 16. Close the connection.
connection.close();
// Step 17. Create a new connection.
connection = cf.createConnection();
// Step 18. Create a new session.
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 19. Try to access the tempQueue2 outside its lifetime
try {
messageConsumer = session.createConsumer(tempQueue2);
throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
} catch (JMSException e) {
System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
}
} finally {
if (connection != null) {
// Step 20. Be sure to close our JMS resources!
connection.close();
}
if (initialContext != null) {
// Step 21. Also close the initialContext!
initialContext.close();
}
}
}
use of javax.jms.TemporaryQueue in project activemq-artemis by apache.
the class DynamicallyIncludedDestinationsDuplexNetworkTest method testTempQueues.
@Test
public void testTempQueues() throws Exception {
TemporaryQueue temp = localSession.createTemporaryQueue();
MessageProducer producer = localSession.createProducer(temp);
producer.send(localSession.createTextMessage("test"));
Thread.sleep(100);
assertEquals("Destination not created", 1, remoteBroker.getAdminView().getTemporaryQueues().length);
temp.delete();
Thread.sleep(100);
assertEquals("Destination not deleted", 0, remoteBroker.getAdminView().getTemporaryQueues().length);
}
use of javax.jms.TemporaryQueue in project activemq-artemis by apache.
the class BasicSecurityTest method testCreateTempDestinationAuthorization.
@Test
public void testCreateTempDestinationAuthorization() throws Exception {
Connection conn1 = null;
Connection conn2 = null;
// Sender
try {
conn1 = factory.createConnection("openwireGuest", "GuEsT");
conn1.start();
conn2 = factory.createConnection("openwireDestinationManager", "DeStInAtIoN");
conn2.start();
Session session1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
session1.createTemporaryQueue();
fail("user shouldn't be able to create temp queue");
} catch (JMSSecurityException e) {
// expected
}
Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue q = session2.createTemporaryQueue();
assertNotNull(q);
} finally {
if (conn1 != null) {
conn1.close();
}
if (conn2 != null) {
conn2.close();
}
}
}
use of javax.jms.TemporaryQueue in project activemq-artemis by apache.
the class SimpleOpenWireTest method testSimpleTempQueue.
@Test
public void testSimpleTempQueue() throws Exception {
AddressSettings addressSetting = new AddressSettings();
addressSetting.setAutoCreateQueues(true);
addressSetting.setAutoCreateAddresses(true);
String address = "#";
server.getAddressSettingsRepository().addMatch(address, addressSetting);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("creating temp queue");
TemporaryQueue tempQueue = session.createTemporaryQueue();
System.out.println("create consumer 1");
MessageConsumer consumer1 = session.createConsumer(tempQueue);
System.out.println("create producer");
MessageProducer producer = session.createProducer(tempQueue);
System.out.println("sending messages");
final int num = 1;
final String msgBase = "MfromAMQ-";
for (int i = 0; i < num; i++) {
TextMessage msg = session.createTextMessage("MfromAMQ-" + i);
producer.send(msg);
System.out.println("Sent a message");
}
// receive
System.out.println("receiving messages...");
for (int i = 0; i < num; i++) {
TextMessage msg = (TextMessage) consumer1.receive(5000);
System.out.println("received: " + msg);
String content = msg.getText();
assertEquals(msgBase + i, content);
}
assertNull(consumer1.receive(500));
session.close();
}
use of javax.jms.TemporaryQueue in project activemq-artemis by apache.
the class ThreeBrokerTempQueueNetworkTest method testTempQueueRecovery.
// this actually uses 4 brokers ...
public void testTempQueueRecovery() throws Exception {
// Setup broker networks
bridgeBrokers("BrokerA", "BrokerB", false, 3);
bridgeBrokers("BrokerB", "BrokerC", false, 3);
startAllBrokers();
BrokerItem brokerItem = brokers.get("BrokerC");
Connection conn = brokerItem.createConnection();
conn.start();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQ = sess.createTemporaryQueue();
Thread.sleep(5000);
for (Iterator<BrokerItem> i = brokers.values().iterator(); i.hasNext(); ) {
BrokerItem bi = i.next();
assertEquals("No queues on broker " + bi.broker.getBrokerName(), 1, bi.broker.getAdminView().getTemporaryQueues().length);
}
createBroker(new URI("broker:(tcp://localhost:61619)/BrokerD?persistent=false&useJmx=true"));
bridgeBrokers("BrokerD", "BrokerA", false, 3);
BrokerItem newBroker = brokers.get("BrokerD");
newBroker.broker.start();
Thread.sleep(1000);
assertEquals("No queues on broker D", 1, newBroker.broker.getAdminView().getTemporaryQueues().length);
tempQ.delete();
Thread.sleep(2000);
for (Iterator<BrokerItem> i = brokers.values().iterator(); i.hasNext(); ) {
BrokerItem bi = i.next();
assertEquals("Temp queue left behind on broker " + bi.broker.getBrokerName(), 0, bi.broker.getAdminView().getTemporaryQueues().length);
}
}
Aggregations