hk2000c技術專欄

          技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks

          郁悶了三天,今天終于把JMS弄出來了,就是發送消息,然后消息監聽器接收到了消息后發送郵件給管理員

          先看web.xml里面關于activemq的invoke

          <!--調用activemq -->
              <context-param >
               <param-name>brokerURI </param-name >
               <param-value>/WEB-INF/activemq.xml </param-value >
              </context-param>
             
              <listener>
                 <listener-class>org.activemq.web.SpringBrokerContextListener</listener-class>
              </listener>


          郁悶了三天,今天終于把JMS弄出來了,就是發送消息,然后消息監聽器接收到了消息后發送郵件給管理員

          先看web.xml里面關于activemq的invoke

          <!--調用activemq -->
              <context-param >
               <param-name>brokerURI </param-name >
               <param-value>/WEB-INF/activemq.xml </param-value >
              </context-param>
             
              <listener>
                 <listener-class>org.activemq.web.SpringBrokerContextListener</listener-class>
              </listener>

          然后是在上下文中定義的JmsTemplate和activemq監聽

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
          http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>

          <!--JMS Template-->
              <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                  <property name="connectionFactory">
                <bean class="org.activemq.ActiveMQConnectionFactory">
                 <property name="brokerURL">
                  <value>tcp://localhost:61616</value>
                 </property>
                </bean>
               </property>
               
                <property name="defaultDestinationName" value="Hello.Queue"/>
              </bean>

             <bean id="activeMQContainer" class="org.activemq.jca.JCAContainer">
               <property name="workManager">
                 <bean id="workManager" class="org.activemq.work.SpringWorkManager"/>
               </property>

               <property name="resourceAdapter">
                 <bean id="activeMQResourceAdapter"
                     class="org.activemq.ra.ActiveMQResourceAdapter">
                   <property name="serverUrl" value="tcp://localhost:61616"/>
                 </bean>
               </property>
             </bean>
           

            <!--監聽 Message 的Message Driven POJO-->
              <bean id="HelloPlaceBean" class="com.officetao.jms.HelloMDP" autowire="byName"/>

            <bean id="HelloMDP" factory-method="addConnector" factory-bean="activeMQContainer">
               <property name="activationSpec">
                 <bean class="org.activemq.ra.ActiveMQActivationSpec">
                   <property name="destination" value="Hello.Queue"/>
                   <property name="destinationType" value="javax.jms.Queue"/>
                 </bean>
               </property>
               <property name="ref" value="HelloBean" />
             </bean>

          </beans>

          建立一個模擬的發送消息的bean,內容如下

          final String mailContent = "新增單號為0000的訂單, 金額";
            try {
                      if (jmsTemplate != null)
                          jmsTemplate.send(new MessageCreator() {
                              public Message createMessage(Session session)
                                      throws JMSException {
                                  Message message = session.createMessage();
                                  message.setStringProperty("content", mailContent);
                                  return message;
                              }
                          });
                  }
                  catch (Exception e) {
                      logger.error("JMS error when place order:", e);
                  }

          最后就是監聽消息然后采取行動的bean


          public class HelloMDP implements MessageListener {


           
           public void onMessage(javax.jms.Message arg0) {
            
            try   {  
                      subAuthenticator   subauth   =   new   subAuthenticator("郵箱登陸名","密碼");//smtp驗證   authenticator  
                      props.put("mail.smtp.host","smtp.163.com");  
                      props.put("mail.smtp.auth","true");  
                      session   =   Session.getInstance(props,subauth);  
                      MimeMessage   message   =   new   MimeMessage(session);  
                      message.setRecipient(Message.RecipientType.TO,new   InternetAddress("
          接收郵件的郵箱"));  
                      message.setFrom(new   InternetAddress("
          自己的郵箱"));  
                      message.setSubject("ok");  
                      message.setText("if you see it,it works!");  
                      Transport.send(message);
                  }  
                  catch(AuthenticationFailedException   e1){  
                      System.out.println("SMTP認證出錯!");  
                  }  
                  catch   (MessagingException   e)   {  
                      e.printStackTrace();
                  }  
           
          }

          public   static   Properties   props   =   System.getProperties();
          public   static   Session   session   =   null;  

          /**  
          *此內部類定義了smtp認證方法  
          */  
          public   class   subAuthenticator   extends   Authenticator{  
          private   String   userName;  
          private   String   password;  
          public   subAuthenticator(String   user,String   pass){  
              userName=user;  
              password=pass;  
          }  
          public   PasswordAuthentication   getPasswordAuthentication(){  
              return   new   PasswordAuthentication(userName,password);  
          }  

          posted on 2007-11-16 16:49 hk2000c 閱讀(1123) 評論(0)  編輯  收藏 所屬分類: JMS
          主站蜘蛛池模板: 台南县| 牙克石市| 邯郸县| 黔西| 伽师县| 博湖县| 永定县| 锦州市| 武城县| 仪征市| 卢氏县| 安仁县| 梅州市| 海南省| 措美县| 土默特右旗| 富民县| 安宁市| 阳信县| 平原县| 呼玛县| 峡江县| 虹口区| 望江县| 宁海县| 台湾省| 寿阳县| 丰宁| 中牟县| 河津市| 南部县| 正定县| 营山县| 黔西| 临沂市| 乡城县| 海城市| 宁海县| 西平县| 当雄县| 丰宁|