Create a spring mvc project
File > New > Spring Project > Spring Mvc Project
Test whether the application is running perfect. If it's running ok you will see a new page openned in browser containing following
If you face errors try to update maven dependencies as
right click on project > maven > update project
Project Structure
gowild.jsp
add a new jsp to view folder
home.jsp
add a link in the home page to new page
New Controller Method
Add a new controller method to home controller page so it will handle new page requests
Testing
Test whether the application running. you should get a new link in the home page which points to the new jsp page.
Pom.xml
Now lets add activemq dependencies through pom.xml. add following to the pom
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.9.0</version>
</dependency>
Sender.java
this class will create message with time stamp send to the local activemq server.
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
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 ...Time is : "+ getTimeInString());
producer.send(message);
System.out.println("Sent: "+message.getText());
} catch (JMSException e){
e.printStackTrace();
}
}
private String getTimeInString(){
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
String formattedDate = dateFormat.format(date);
return formattedDate;
}
}
Receiver.java
import javax.jms.Connection;This class will read the messages from local activemq.
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.2");
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();
System.out.println(e.getErrorCode());
}
}
}
Execution
Above activemq admin page will show messages in the samplequeue. inorder to consume messags we will have to manually move messages to q 2.
click on q 1
click on first message
in the message section in the drop down select q 2 and click on move link
Now if you click on here link on home page you will see that message been consumed.
Comments
Post a Comment