Spring Jms入門實例

Spring JMS 
        Spring框架提供了一個模板機制來隱藏Java APIs的細節。開發人員可以使用JDBCTemplate和JNDITemplate類來分別訪問後臺數據庫和JEE資源(數據源,連接池)。JMS也不例外,Spring提供JMSTemplate類,因此開發人員不用爲一個JMS實現去編寫樣本代碼。接下來是在開發JMS應用程序時Spring所具有一些的優勢。 

提供JMS抽象API,簡化了訪問目標(隊列或主題)和向指定目標發佈消息時JMS的使用。 
開發人員不需要關心JMS不同版本(例如JMS 1.0.2與JMS 1.1)之間的差異。 
開發人員不必專門處理JMS異常,因爲Spring爲所有JMS異常提供了一個未經檢查的異常,並在JMS代碼中重新拋出。 
實戰:下面是我在學習過程中的一個入門實例 

1.  在web.xml文件中配置一個spring用的上下文:

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/jmsconfig.xml</param-value>  
</context-param>  
<!-- 配置Spring容器 -->  
<listener>  
    <listener-class>  
           org.springframework.web.context.ContextLoaderListener  
       </listener-class>  
</listener>

2.  創建jmsconfig.xml用來裝配jms,內容如下:

<?xml version="1.0" encoding="UTF-8"?>     
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
    "http://www.springframework.org/dtd/spring-beans.dtd">     
  
<beans>  
    <bean id="jmsConnectionFactory"  
        class="org.springframework.jndi.JndiObjectFactoryBean">  
        <property name="jndiName">  
            <value>UIL2ConnectionFactory</value>  
        </property>  
    </bean>  
      
    <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">  
        <property name="jndiName">  
            <value>topic/scJms</value>  
        </property>  
    </bean>  
      
    <!--  Spring JmsTemplate config -->  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory">  
            <bean  
                class="org.springframework.jms.connection.SingleConnectionFactory">  
                <property name="targetConnectionFactory"  
                    ref="jmsConnectionFactory"/>  
            </bean>  
        </property>  
    </bean>  
      
    <!-- POJO which send Message uses  Spring JmsTemplate --> <!--配置消息發送者-->  
    <bean id="messageProducer" class="co.transport.jms.MessageProducer">  
        <property name="template" ref="jmsTemplate"/>  
        <property name="destination" ref="destination"/>  
    </bean>  
      
    <!--  Message Driven POJO (MDP) -->  
    <bean id="messageListener" class="co.transport.jms.MessageConsumer"/>  
      
    <!--  listener container,MDP無需實現接口 -->  
    <bean id="listenerContainer"  
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
        <property name="connectionFactory" ref="jmsConnectionFactory"/>  
        <property name="destination" ref="destination"/>  
        <property name="messageListener" ref="messageListener"/>  
    </bean>  
</beans>  

其中: 
        1)   jmsConnectionFactory用的是Jboss自帶的一個ConnectionFactory,可以在Jboss的deploy/jms目錄下的uil2-service.xml文件中找到。 
      2)  destination使用自定義的,需要在Jboss加一個配置文件,下面會介紹。 
      3)  MessageProducer是消息發送方 
      4)  MessageConsumer實現了一個MessageListener,監聽是否收到消息。 

3.  發送和接收消息的class如下: 

MessageProducer.java  
  
public class MessageProducer {  
    private JmsTemplate template;  
  
    private Destination destination;  
  
    public void send(final String message) {  
        template.send(destination, new MessageCreator() {  
            public Message createMessage(Session session) throws JMSException {  
                Message m = session.createTextMessage(message);  
                return m;  
            }  
        });  
    }  
  
    public void setDestination(Destination destination) {  
        this.destination = destination;  
    }  
  
    public void setTemplate(JmsTemplate template) {  
        this.template = template;  
    }  
  
}  
MessageConsumer.java  
  
public class MessageConsumer implements MessageListener {  
  
    public void onMessage(Message message) {  
        System.out.println("****************************************");  
        System.out.println(message);  
    }  
}  

4.  寫一個Servlet作爲測試

JmsTest.java(不要忘記在web.xml中配置這個servlet哦!)

public class JmsTest extends HttpServlet {  
  
    public void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        PrintWriter pw = response.getWriter();  
        pw.println("<html>");  
        pw.println("<head>");  
        pw.println("</head>");  
        pw.println("<body>");  
        try {  
            ServletContext servletContext = this.getServletContext();  
            WebApplicationContext wac = WebApplicationContextUtils  
                    .getRequiredWebApplicationContext(servletContext);  
            MessageProducer mp = (MessageProducer) wac.getBean("messageProducer");  
  
            mp.send("Message!!!!");  
            pw.println("<h3>success</h3>");  
        } catch (JmsException e) {  
            pw.println(e);  
        }  
        pw.println("</body>");  
        pw.println("</html>");  
    }  
  
}  

5.  在Jboss的deploy/jms目錄下創建sc-jms-service.xml 
<?xml version="1.0" encoding="UTF-8"?>     
<server>     
    <mbean code="org.jboss.mq.server.jmx.Topic"     
         name="jboss.mq.destination:service=Topic,name=somcJms">     
        <depends optional-attribute-name="DestinationManager">  
            jboss.mq:service=DestinationManager  
        </depends>     
        <depends optional-attribute-name="SecurityManager">  
            jboss.mq:service=SecurityManager  
        </depends>     
        <attribute name="SecurityConf">     
            <security>     
                <role name="guest" read="true" write="true"/>     
                <role name="publisher" read="true" write="true" create="false"/>     
                <role name="durpublisher" read="true" write="true" create="true"/>     
            </security>     
        </attribute>     
    </mbean>     
</server>  

6.  將工程達成war包直接放到Jboss的deploy目錄下即可,然後啓動Jboss訪問JmsTest這個Servlet,你會在控制檯看到接受到的消息,至此,一個最簡單的Jms實例完成了 

轉載:http://zhangli-lisa.iteye.com/blog/464586

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章