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 阅读(1105) 评论(0)  编辑  收藏 所属分类: JMS

只有注册用户登录后才能发表评论。


网站导航: