In this scenario, User could send Message in the html page, and the backend thread would receive the message and do some business logic.
1. the pom for the ActiveMq jar libs and SpringMVC:
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-beans
${org.springframework.version}
org.springframework
spring-web
${org.springframework.version}
cglib
cglib
2.2
org.springframework
spring-jms
${org.springframework.version}
org.apache.activemq
activemq-all
5.8.0
2. the applicationContext.xml,which define the JMS resouces as beans:
class="com.wushi.mqtest.service.ReceiveMessageListener">
class="org.springframework.jms.listener.DefaultMessageListenerCo
ntainer">
3. the web.xml,which define the Spring IOC and SpringMVC:
mqtest
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:mvc.xml
1
springmvc
*.w
4. the mvc.xml, which defined the SpringMVC configuration:
5. the MessageSenderInf and MessageSender:
public interface MessageSenderInf {
public void sendMessage(final String message);;
}
@Service("messageSender")
public class MessageSender implements MessageSenderInf {
final Logger myLog = Logger.getLogger(MessageSender.class);
@Autowired
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final String message) {
myLog.info("Send message: " + message);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
}
}
6. the message consumer:
public class ReceiveMessageListener implements MessageListener {
public void onMessage(Message msg) {
final Logger myLog = Logger.getLogger(ReceiveMessageListener.class);
if(msg instanceof TextMessage){
TextMessage text=(TextMessage)msg;
try{
myLog.info("receive the message: "+text.getText());
}catch(Exception e){
e.printStackTrace();
}
}
}
}
7. the tomcat configration in context.xml, which defined the JNDI resources for ConnectionFactory and Queue( there is not JNDI server in this demo , so we could define JNDI resources in context.mxl to simulate the JNDI service):
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactor
y"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="MyActiveMQBroker"/>
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="My Message Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="MyMessageQueue"/>
8. run the demo:
a. start the ActiveMq
b. start tomcat
c. goin to the SpringMVC web page, and send message.
d. go into the ActiveMq web console to verify the message.
1. the pom for the ActiveMq jar libs and SpringMVC:
2. the applicationContext.xml,which define the JMS resouces as beans:
3. the web.xml,which define the Spring IOC and SpringMVC:
4. the mvc.xml, which defined the SpringMVC configuration:
5. the MessageSenderInf and MessageSender:
public interface MessageSenderInf {
}
@Service("messageSender")
public class MessageSender implements MessageSenderInf {
}
6.
}
7. the tomcat configration in context.xml, which defined the JNDI resources for ConnectionFactory and Queue( there is not JNDI server in this demo , so we could define JNDI resources in context.mxl to simulate the JNDI service):
8. run the demo: