ActiveMq1: Run Stand Alone.

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值