| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
| <title>ActiveMQ</title> |
| <link rel="icon" type="image/png" href="/assets/img/favicon.png"> |
| |
| <link rel="stylesheet" href="/css/main.css"> |
| <script defer src="/js/fontawesome-all.min.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc"></script> |
| <script src="/js/jquery.slim.min.js" integrity="sha384-5AkRS45j4ukf+JbWAfHL8P4onPA9p0KwwP7pUdjSQA3ss9edbJUJc/XcYAiheSSz"></script> |
| <script src="/js/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"></script> |
| <script src="/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"></script> |
| </head> |
| |
| <body> |
| <nav class="navbar navbar-expand-lg navbar-light fixed-top"> |
| <div class="container"> |
| <!-- <a class="navbar-brand mr-auto" href="#"><img style="height: 50px" src="assets/img/apache-feather.png" /></a> --> |
| <a class="navbar-brand mr-auto" href="/"><img src="/assets/img/activemq_logo_black_small.png" style="height: 50px"/></a> |
| <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#navbarContent" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation"> |
| <span class="navbar-toggler-icon"></span> |
| </button> |
| |
| <div class="ml-auto collapse navbar-collapse" id="navbarContent"> |
| <ul class="navbar-nav ml-auto"> |
| <li class="nav-item"> |
| <a class="nav-link active" href="/news">News</a> |
| </li> |
| <li class="nav-item dropdown"> |
| <a class="nav-link" id="navbarDropdownComponents" data-target="#" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Components<span class="caret"></span></a> |
| <ul class="dropdown-menu dropdown-menu-center" aria-labelledby="navbarDropdownComponents"> |
| <div class="row"> |
| <div class="col-12"> |
| <ul class="multi-column-dropdown"> |
| <li class="nav-item"><a class="dropdown-item" href="/components/classic">ActiveMQ Classic</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/components/artemis/">ActiveMQ Artemis</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/components/nms">NMS Clients</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/components/cms">CMS Client</a></li> |
| </ul> |
| </div> |
| </div> |
| </ul> |
| </li> |
| <li class="nav-item dropdown"> |
| <a class="nav-link" id="navbarDropdownCommunity" data-target="#" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Community<span class="caret"></span></a> |
| <ul class="dropdown-menu dropdown-menu-center multi-column columns-1" aria-labelledby="navbarDropdownCommunity"> |
| <div class="row"> |
| <div class="col-12"> |
| <ul class="multi-column-dropdown"> |
| <li class="nav-item"><a class="dropdown-item" href="/contact">Contact Us</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/contributing">Contribute</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/issues">Report Issues</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/support">Get Support</a></li> |
| </ul> |
| </div> |
| </div> |
| </ul> |
| </li> |
| <li class="nav-item dropdown"> |
| <a class="nav-link" id="navbarDropdownTeam" data-target="#" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><img src="/assets/img/feather.png" style="height:20px">Apache<span class="caret"></span></a> |
| <ul class="dropdown-menu dropdown-menu-center multi-column columns-1" aria-labelledby="navbarDropdownTeam"> |
| <div class="row"> |
| <div class="col-sm-12"> |
| <ul class="multi-column-dropdown"> |
| <li class="nav-item"><a class="dropdown-item" href="https://www.apache.org">The Apache Software Foundation</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://www.apache.org/licenses/">License</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="/security-advisories">Security</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://www.apache.org/events/current-event">Events</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://people.apache.org/phonebook.html?pmc=activemq">PMC & Committers</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://whimsy.apache.org/board/minutes/ActiveMQ.html">Board Reports</a></li> |
| <li class="nav-item"><a class="dropdown-item" href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a></li> |
| </ul> |
| </div> |
| </div> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </nav> |
| |
| <div class="content"> |
| <div class="page-title-classic"> |
| <div class="container"> |
| <h1>Hello World</h1> |
| </div> |
| </div> |
| <div class="container" > |
| <div class="row" style="margin-top: 30px"> |
| <div class="col-12 classic"> |
| <p><a href="using-activemq-classic">Using ActiveMQ</a> > <a href="hello-world">Hello World</a></p> |
| |
| <p>The following is a very simple JMS application with multiple, concurrent, consumers and producers. See the <a href="initial-configuration">Initial Configuration</a> guide for details on how to setup your classpath correctly.</p> |
| |
| <p>Things you might do after running this example:</p> |
| |
| <ul> |
| <li><a href="run-broker">Setup a broker</a> instead of using the org.activemq.broker.impl.Main class directly</li> |
| <li>Use JNDI to lookup a javax.jms.ConnectionFactory rather than creating ActiveMQConnectionFactory directly.</li> |
| <li>Implement the javax.jms.MessageListener interface rather than calling consumer.receive()</li> |
| <li>Use transactional sessions</li> |
| <li>Use a Topic rather than a queue</li> |
| </ul> |
| |
| <p>The point of this example is to show you the basic code required to use JMS in a straightforward way. The Consumers and Producers could very easy be on completely different machines or in different processes.</p> |
| |
| <p><strong>App.java</strong></p> |
| <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import org.apache.activemq.ActiveMQConnectionFactory; |
| |
| import javax.jms.Connection; |
| import javax.jms.DeliveryMode; |
| import javax.jms.Destination; |
| import javax.jms.ExceptionListener; |
| import javax.jms.JMSException; |
| import javax.jms.Message; |
| import javax.jms.MessageConsumer; |
| import javax.jms.MessageProducer; |
| import javax.jms.Session; |
| import javax.jms.TextMessage; |
| |
| /** |
| * Hello world! |
| */ |
| public class App { |
| |
| public static void main(String\[\] args) throws Exception { |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| Thread.sleep(1000); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| Thread.sleep(1000); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldProducer(), false); |
| Thread.sleep(1000); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldConsumer(), false); |
| thread(new HelloWorldProducer(), false); |
| } |
| |
| public static void thread(Runnable runnable, boolean daemon) { |
| Thread brokerThread = new Thread(runnable); |
| brokerThread.setDaemon(daemon); |
| brokerThread.start(); |
| } |
| |
| public static class HelloWorldProducer implements Runnable { |
| public void run() { |
| try { |
| // Create a ConnectionFactory |
| ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); |
| |
| // Create a Connection |
| Connection connection = connectionFactory.createConnection(); |
| connection.start(); |
| |
| // Create a Session |
| Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| |
| // Create the destination (Topic or Queue) |
| Destination destination = session.createQueue("TEST.FOO"); |
| |
| // Create a MessageProducer from the Session to the Topic or Queue |
| MessageProducer producer = session.createProducer(destination); |
| producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); |
| |
| // Create a messages |
| String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode(); |
| TextMessage message = session.createTextMessage(text); |
| |
| // Tell the producer to send the message |
| System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName()); |
| producer.send(message); |
| |
| // Clean up |
| session.close(); |
| connection.close(); |
| } |
| catch (Exception e) { |
| System.out.println("Caught: " + e); |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| public static class HelloWorldConsumer implements Runnable, ExceptionListener { |
| public void run() { |
| try { |
| |
| // Create a ConnectionFactory |
| ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); |
| |
| // Create a Connection |
| Connection connection = connectionFactory.createConnection(); |
| connection.start(); |
| |
| connection.setExceptionListener(this); |
| |
| // Create a Session |
| Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| |
| // Create the destination (Topic or Queue) |
| Destination destination = session.createQueue("TEST.FOO"); |
| |
| // Create a MessageConsumer from the Session to the Topic or Queue |
| MessageConsumer consumer = session.createConsumer(destination); |
| |
| // Wait for a message |
| Message message = consumer.receive(1000); |
| |
| if (message instanceof TextMessage) { |
| TextMessage textMessage = (TextMessage) message; |
| String text = textMessage.getText(); |
| System.out.println("Received: " + text); |
| } else { |
| System.out.println("Received: " + message); |
| } |
| |
| consumer.close(); |
| session.close(); |
| connection.close(); |
| } catch (Exception e) { |
| System.out.println("Caught: " + e); |
| e.printStackTrace(); |
| } |
| } |
| |
| public synchronized void onException(JMSException ex) { |
| System.out.println("JMS Exception occured. Shutting down client."); |
| } |
| } |
| } |
| </code></pre></div></div> |
| |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="row sitemap"> |
| <div class="col-sm-12"> |
| <div class="container"> |
| <div class="row"> |
| <div class="col-sm-12"> |
| <div class="row"> |
| <div class="col-sm-3"> |
| <div > |
| <img class="float-left" style="max-height: 100px" src="/assets/img/activemq_logo_white_vertical_small.png"/> |
| </div> |
| </div> |
| <div style="text-align: center; margin-bottom: 0px; margin-top: 30px; font-size: 65%" class="col-sm-6"> |
| <p><a href="https://www.apache.org/foundation/marks/list/">Apache, ActiveMQ, Apache ActiveMQ</a>, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2024, The Apache Software Foundation. Licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a>.</p> |
| </div> |
| <div class="col-sm-3"> |
| <div > |
| <a href="https://www.apache.org"><img class="float-right" style="margin-top: 10px; max-height: 80px" src="/assets/img/apache-logo-small.png"/></a> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| |
| </body> |
| </html> |