activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thufir <hawat.thu...@gmail.com>
Subject hello world doesn't work against server. runs stand-alone only.
Date Mon, 09 Mar 2015 10:22:51 GMT
I'm still learning activemq.  This sample seems to run ok, a sort of 
hello world.  However, it doesn't run so well when I actually start 
activemq.  Is that because activemq is "embedded" in some way?


thufir@doge:~$
thufir@doge:~$ activemq status
INFO: Loading '/home/thufir/apache-activemq-5.11.1/bin/env'
INFO: Using java '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'
ActiveMQ not running
thufir@doge:~$
thufir@doge:~$ java -jar NetBeansProjects/ActiveMQ/dist/ActiveMQ.jar
log4j:WARN No appenders could be found for logger 
(org.apache.activemq.broker.jmx.ManagementContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for 
more info.
Sent message: 309334921 : Thread-1
Sent message: 1392385769 : Thread-9
Sent message: 2081591507 : Thread-0
Sent message: 1374783350 : Thread-11
Received: Hello world! From: Thread-1 : 445134555
Received: Hello world! From: Thread-11 : 1299279378
Received: Hello world! From: Thread-9 : 652364872
Sent message: 591774847 : Thread-13
Received: Hello world! From: Thread-13 : 265803401
Sent message: 1207962228 : Thread-17
Sent message: 164184341 : Thread-16
Received: Hello world! From: Thread-17 : 2122895298
Received: Hello world! From: Thread-16 : 1861409291
Sent message: 213493319 : Thread-25
Sent message: 1288767035 : Thread-27
Received: Hello world! From: Thread-25 : 1393581690
Sent message: 489077211 : Thread-29
Received: Hello world! From: Thread-29 : 979410438
Sent message: 1826381686 : Thread-22
Received: Hello world! From: Thread-27 : 573011569
Received: Hello world! From: Thread-22 : 1834164732
Sent message: 387902785 : Thread-32
Received: Hello world! From: Thread-32 : 2116585620
Received: null
thufir@doge:~$
thufir@doge:~$ activemq start
INFO: Loading '/home/thufir/apache-activemq-5.11.1/bin/env'
INFO: Using java '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and 
log4j.properties to get details
INFO: pidfile created : 
'/home/thufir/apache-activemq-5.11.1/data/activemq.pid' (pid '14357')
thufir@doge:~$
thufir@doge:~$ activemq status
INFO: Loading '/home/thufir/apache-activemq-5.11.1/bin/env'
INFO: Using java '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'
ActiveMQ is running (pid '14357')
thufir@doge:~$
thufir@doge:~$ java -jar NetBeansProjects/ActiveMQ/dist/ActiveMQ.jar
log4j:WARN No appenders could be found for logger 
(org.apache.activemq.broker.jmx.ManagementContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for 
more info.
Sent message: 454239699 : Thread-9
Sent message: 629808856 : Thread-1
Sent message: 102256008 : Thread-11
Sent message: 1079965849 : Thread-0
Received: Hello world! From: Thread-11 : 966984015
Received: Hello world! From: Thread-1 : 1031698099
Received: Hello world! From: Thread-9 : 2091583256
Sent message: 490664783 : Thread-13
Sent message: 973112065 : Thread-16
Sent message: 495518379 : Thread-17
Received: Hello world! From: Thread-13 : 2122469300
Received: Hello world! From: Thread-17 : 1046619508
Received: Hello world! From: Thread-16 : 963084272
Sent message: 2137767383 : Thread-22
Sent message: 267200496 : Thread-25
Sent message: 409357971 : Thread-32
Sent message: 530595976 : Thread-27
Sent message: 173311637 : Thread-29
Received: Hello world! From: Thread-22 : 643720646
Received: Hello world! From: Thread-32 : 191300790
Received: Hello world! From: Thread-25 : 2054991771
Received: Hello world! From: Thread-27 : 692178825
Received: Hello world! From: Thread-29 : 1374466288
Received: null
thufir@doge:~$
thufir@doge:~$ cat NetBeansProjects/ActiveMQ/src/foo/App.java
package foo;

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.");
         }
     }
}
thufir@doge:~$
thufir@doge:~$


thanks,

Thufir

Mime
View raw message