// Create a connection factory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Create a connection Connection connection = connectionFactory.createConnection(); // Start the connection connection.start(); // Create a session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Get the queue Destination queue = session.createQueue("myqueue"); // Create a message producer MessageProducer producer = session.createProducer(queue); // Create a message Message message = session.createTextMessage("Hello, World!"); // Send the message producer.send(message); // Close the connection connection.close();
// Create a connection factory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Create a connection Connection connection = connectionFactory.createConnection(); // Start the connection connection.start(); // Create a session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Get the queue Destination queue = session.createQueue("myqueue"); // Create a message consumer MessageConsumer consumer = session.createConsumer(queue); // Receive the message Message message = consumer.receive(); // Cast the message to a TextMessage TextMessage textMessage = (TextMessage) message; // Print the message System.out.println("Received message: " + textMessage.getText()); // Close the connection connection.close();In both examples, the ActiveMQ library is being used to create the connection factory and the queue. The messages are being sent and received using the JMS API, which provides a standard way of interacting with messaging systems.