Spring JMS POJOs - P.2

Sử dụng Spring JMS phần 2

Sau đây là phần thứ 2, cấu hình cho hệ thống, và chạy chương trình.

Cấu hình Message-Driven POJO

Bây giờ đến phần quan trọng nhất. Chúng ta sẽ cấu hình POJO service như thế nào để nó nhận một JMS message? Câu trả lời đến từ việc định nghĩa 2 bean(tính là 3 nếu tính luôn cả bản thân service).

Trong file định định nghĩa bean kế tiếp, lưu ý rằng "container" thật sự nhận thông điệp và cho phép sử dụng một listerner bất đồng bộ.

Container cần nhận biết được connectionFactory và Destination từ thông điệp nó nhận.

Listener trong trường hợp này là một thể hiện của Spring MessageListenerAdapter. Nó tham chiếu tới delegate(POJO service) và tên của phương thức xử lý. Trong trường hợp này, chúng ta cũng cung cấp một defaultResponseDestination. Trả về void.

servlet-context.xml

<?xml version="1.0" encoding="UTF-8" ? >

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd" >

<import resource="shared-context.xml"/ >

<bean id="registrationService" class="blog.mdp.RegistrationService" / >

<bean id="listener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter" >

<property name="delegate" ref="registrationService" / >

<property name="defaultListenerMethod" value="processRequest" / >

<property name="defaultResponseDestination" ref="replyQueue" / >

</bean>

<bean id="container" class="org.springframework.jms.listener.SimpleMessageListenerContainer">

<property name="connectionFactory" ref="connectionFactory" / >

<property name="messageListener" ref="listener" / >

<property name="destination" ref="requestQueue" / >

</bean >

</beans >

Bước cuối cùng chúng ta phải cung cấp một cơ chế boostrap để chạy dịch vụ (hiểu đơn giản là khởi động sevice trên server). Vì đây là một ví dụ nên tôi sẽ làm theo cách đơn giản nhất, chúng ta tạo class RegistrationServiceRunner.

package blog.mdp;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RegistrationServiceRunner {

public static void main(String[] args) throws IOException {

new ClassPathXmlApplicationContext("/blog/mdp/server-context.xml");

System.in.read();

}

}

Cấu hình Client.

Trên Client, chúng ta sẽ gửi thông tin đăng ký và ghi chép lại thông điệp phản hồi. (đầu tiên, chúng ta liệt kê các danh sách bean. Sau khi đọc xong, bạn nên tìm hiểu thêm về container, listener.

Trong chương trình Delegate là ReplyNotifier sẽ trả về void. Tôi đặt tên file là client-context.xml.

client-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<import resource="shared-context.xml" / >

<bean id="replyNotifier" class="blog.mdp.ReplyNotifier" / >

<bean id="listener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">

<property name="delegate" ref="replyNotifier" / >

<property name="defaultListenerMethod" value="notify" / >

</bean>

<bean id="container" class="org.springframework.jms.listener.SimpleMessageListenerContainer">

<property name="connectionFactory" ref="connectionFactory" / >

<property name="messageListener" ref="listener" / >

<property name="destination" ref="replyQueue" / >

</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

<property name="connectionFactory" ref="connectionFactory" / >

<property name="defaultDestination" ref="requestQueue" / >

</bean>

</beans>

Tôi sẽ tạo thêm một bean khác, là một chương trình để chạy dưới client, nó là một thể hiện của Spring jmsTemplate. Tôi sẽ sử dụng chúng để gửi đi những thông điệp đến defaultDestination. Gửi thông điệp dạng text với convertAndSend() method mà Spring cung cấp.

package blog.mdp;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jms.core.JmsTemplate;

public class RegistrationConsole {

public static void main(String[] args) throws IOException {

ApplicationContext context = new ClassPathXmlApplicationContext("/blog/mdp/client-context.xml");

JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

for (;;) {

System.out.print("To Register, Enter Name: ");

String name = reader.readLine();

RegistrationRequest request = new RegistrationRequest(name);

jmsTemplate.convertAndSend(request);

}

}

}

Và cuối cùng, hãy chiêm ngưỡng kết quả của chúng ta.

Phần thú vị nhất và cũng là......hồi hộp nhất, Trước tiên bạn chạy ActiveMQ broker (xem phần cài đặt môi trường)

(Chạy activeMQ có nhiều cách khác nhau, bạn có thể download phiên bản của activeMQ và chạy file trong thư mục bin, cũng có thể bạn chọn cách cấu hình vm:localhost:61616. Ở đây, đảm bảo vấn đề về phân tán và đơn giản, bạn sẽ chạy org.activemq.broker.impl.Main. Protocol là tcp

(Hình thứ nhất)

Như bạn thấy dịch vụ được chạy trên máy nangvang đã được khởi động trên port 61616

Chạy hàm main của RegistrationServiceRunner nhận yêu cầu.(hình 2)

Khởi tạo dịch vụ trên server

Sau đó chạy hàm main của RegistrationConsole dưới client. Theo đó chương trình sẽ yêu cầu bạn nhập tên, sau khi nhập tên bằng console, thông điệp trả về, nếu không. Chuẩn bị tinh thần debug.

Ở đây, tên đầu tiên tôi nhập là : Dang Tri Tue thông điệp được trả về là not confirmed, rất đơn giản là trong phần xử lý thông điệp tôi xử lý như thế, nó hàm chứa việc xử lý lỗi.

Sau đó tôi nhập tên là : Tran Trung Tin thông tin được nhận là confirm

Chúc mừng Spring JMS của bạn.

1.

About Langthang

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments :

  1. chao anh !
    Em doc bai viet cua anh rat hay va kien thuc rong !
    Em dang tim hieu ve phan ActiveMQ nhung em co it tai lieu qua va cung khong biet tim hieu tu dau
    tu trang activemq.apache.org thi em download sourcecode ve roi nhung tai lieu thi em thay chi co nhung file javadoc .
    em muon tim hieu ve chuc nang va thiet ke cua no
    lieu anh co the giup em khong !

    neu anh co tai lieu thi gui cho em voi nhe
    email : manhhungv@gmail.com

    thanks anh nhieu !

    ReplyDelete