activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "AMARNATH, Balachandar" <BALACHANDAR.AMARN...@airbus.com>
Subject RE: Conntecting to a remote broker.
Date Tue, 26 Feb 2013 05:37:54 GMT
HelloWorldConsumer.java
**************************


import java.util.ArrayList;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class HelloWorldConsumer {
	
        //public void run() {
	     public static void main(String[] args ){
            try {

            	String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  				
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
                
                // 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 MessageConsumer from the Session to the Topic or Queue
                MessageConsumer consumer = session.createConsumer(destination);
                // Wait for a message
                
                ArrayList<String> contents = new ArrayList<String>();
                boolean flag = true;
                
                while (flag){
                	Message message = consumer.receive(1000);
                	
                
                
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    String text = textMessage.getText();
                    
                    System.out.println("Received : " + text + " and processing");
                    //Thread.sleep(1500);                    
                    
                    if (text.equalsIgnoreCase("END")){
                    	System.out.println("The recvd text is "+text);                    
                    	flag = false;
                    }
                    contents.add(text);
                } else {                	
                	System.out.println("Received : "+message);                
                }   
                
                }   
                System.out.println("Exiting..");
                System.out.println("The contents collected in consumer.java is "+contents.size());
                connection.close();
                
            } catch (Exception e) {
                System.out.println("Caught: " + e);
                e.printStackTrace();
            }
        }	
}

HelloWorldProducer.java
**************************

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class HelloWorldProducer {
	
	public static void main(String[] args) throws Exception {
		
		 		String url = ActiveMQConnection.DEFAULT_BROKER_URL;
      				
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
                
                // 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);
                
                int counter = 0;
                boolean flag = true; 
                
                while (counter < 50)
                {
                	
                counter++;
                String text = "Message "+counter;// + Thread.currentThread().getName() + "
: " + this.hashCode();
                TextMessage message = session.createTextMessage(text);                
                // Tell the producer to send the message
                
                producer.send(message);        
                
                System.out.println("Sent message '" + message.getText() + "'");
                Thread.sleep(1000);
                         
                }
                Thread.sleep(2000);
                producer.send(session.createTextMessage("END"));
                
                Thread.sleep(4000);
                producer.send(session.createTextMessage("END"));
                // Clean up                
                connection.close();
        
        }
    }


Hope this helps :-)


With regards
Bala


-----Original Message-----
From: Christian Posta [mailto:christian.posta@gmail.com] 
Sent: 26 February 2013 05:15
To: users@activemq.apache.org
Subject: Re: Conntecting to a remote broker.

A few seconds on google turns up this excellent blog post that perfectly
answers your question:

http://www.javablogging.com/simple-guide-to-java-message-service-jms-using-activemq/

Follow it step by step and you'll be on your way.
Cheers


On Mon, Feb 25, 2013 at 1:24 PM, iluv2salsa <ted_shear@yahoo.com> wrote:

> I am in need of a simple example to do the following:
> On one Fedora 18 machine create a broker and a consumer to recieve a
> message.
> On another Fedora 18 machine create a producer and send a message.
>
> i have been trying for a couple of weeks and spend many hours on google.
>
> Please, nothing fancy, no jboss, glassfish, etc.
>
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/Conntecting-to-a-remote-broker-tp4664053.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>



-- 
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta

This mail has originated outside your organization, either from an external partner or the
Global Internet.
Keep this in mind if you answer this message.



The information in this e-mail is confidential. The contents may not be disclosed or used
by anyone other than the addressee. Access to this e-mail by anyone else is unauthorised.
If you are not the intended recipient, please notify Airbus immediately and delete this e-mail.
Airbus cannot accept any responsibility for the accuracy or completeness of this e-mail as
it has been sent over public networks. If you have any concerns over the content of this message
or its Accuracy or Integrity, please contact Airbus immediately.
All outgoing e-mails from Airbus are checked using regularly updated virus scanning software
but you should take whatever measures you deem to be appropriate to ensure that this message
and any attachments are virus free.


Mime
View raw message