Pom.xml
Now open pom.xml and add the required dependencies.<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.activemq</groupId>
<artifactId>activeio-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.7</version>
</dependency>
add dependency activemq-all version 5. while building if you get a error saying activeio-core is missing add a exclusion. If its required xbean dependency add as above.
Sender.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;
public void sendMessage(){
try{
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("SAMPLEQUEUE");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a sample message..sending from FirstClient");
producer.send(message);
System.out.println("Sent: "+message.getText());
} catch (JMSException e){
e.printStackTrace();
}
}
}
Create send class as above.
App.java
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Sender sender = new Sender();
sender.sendMessage();
}
}
Add above code to the main method.
Executing
Now start your local ActiveMq up and running.execute your application.
go to admin view of activemq browser
you will see a new message there.
Our queue name is SAMPLEQUEUE
Receiver.java
lets create the consumer now.import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
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 Receiver {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageConsumer consumer = null;
public void receiveMessage() {
factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_BROKER_URL);
try {
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("SAMPLEQUEUE");
consumer = session.createConsumer(destination);
Message message = consumer.receive();
if (message instanceof TextMessage){
TextMessage textmessage = (TextMessage)message;
System.out.println("Message is : "+ textmessage.getText());
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
App.java
update the main class.public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Sender sender = new Sender();
sender.sendMessage();
System.out.println( "Hello Receiver!" );
Receiver receiver = new Receiver();
receiver.receiveMessage();
}
}
Execution 2
in the queues tab in the admin view of ActiveMQ page, will allow us to show what is happening.
Enqueue count - number of messages added to the queue
Dequeue count - number of messages removed from the queue
Comments
Post a Comment