JMS LÀ GÌ

  -  

1. Giới thiệu JMS

a. Nhắn tin là gì?

Là việc lên tiếng thân các yếu tố hệ thống xuất xắc các vận dụng không giống nhau (vào môi trường phân tán) không được liên kết ngặt nghèo nlỗi giao thức TCPhường., CORBA, xuất xắc RXiaoMI.- Ưu điểm của nhắn tin là kĩ năng tích thích hợp những nền tảng gốc rễ khác biệt, có tác dụng sút tắc nghẽn hệ thống, bức tốc kĩ năng không ngừng mở rộng và nâng cao độ tin tưởng gửi nhấn tin.

Bạn đang xem: Jms là gì

b. Mô hình của nhắn tin

*

c. JMS là gì?

Java Message Service (JMS) API là một phần của kỹ thuật Java Enterprice Edition (JEE). JMS là toàn bộ hầu như gì thuộc về Việc gửi cùng nhận tin thân nhị giỏi những client.- Nó là 1 trong Đặc điểm kỹ thuật biểu đạt một cách tiến hành chế tạo bởi vì trương trình Java cho câu hỏi sinh sản, gửi với dìm lời nhắn.- JMS API cho phép lới lỏng việc links ban bố, cùng gửi tin nhắn một giải pháp bất đồng nhất.

d. Kiến trúc một áp dụng JMS

*

Bao bao gồm các thành phần:

Producer/Publisher: Thành phần chế tạo ra và gửi tinBroker trung gian tốt Message Oriented Middleware (MOM)

Consumer/Subcriber: Thành phần nhấn tinProducer và Consumer là gần như ứng dụng Java. Còn MOM là vận dụng trung gian.

Một số MOM tiêu biểu:

Webxúc tích -Oracle

MQSeries- IBM

JBOSSMQ - JBOSS

SonigMQ - Progress

TIBCO EMS- TIBCO

ActiveMQ- Apache

e. JMS cũng đều có 2 mô hình nhắn tin là PTP cùng Pub/Sub

*

Tại sơ vật dụng điểm – điểm (PTP) bao gồm bố nhân tố chính là ứng dụng gửi, Queue, ứng dụng dìm tin. Mỗi tin nhắn được gửi đến một client ví dụ. Queue bảo quản các lời nhắn cho tới lúc client thừa nhận không còn hoặc cho thời hạn timout tùy chỉnh cấu hình.

*

Tại sơ trang bị Pub/Sub bên trên cũng có ba thành phần chính là phía gửi, Topic, và phía nhận.

Mỗi tin nhắn hoàn toàn có thể được nhận do những client. Topic vẫn giữ giàng toàn bộ tin nhắn nhưng không bị mất đi cho đến Khi MOM được reset, giỏi xóa.

Xem thêm: 7 Bước Lập Kế Hoạch Marketing Cho Sản Phẩm Mới 2020, 7 Bước Lập Kế Hoạch Marketing Sản Phẩm Mới 2020

2.Gới thiệu ActiveMQ

a.ActiveMQ là gì?

ActiveMQ là một MOM mã nguồn mlàm việc, phổ biến cùng vượt trội nhất.

ActiveMQ hoàn toàn có thể chạy tự do, giỏi phía bên trong các quy trình không giống, áp dụng VPS, tốt ứng dụng JEE.

Hỗ trợ phần lớn sản phẩm JMS yên cầu, với có thể không ngừng mở rộng.- Ngoài Java thì ActiveMQ hoàn toàn có thể áp dụng với .NET, C/C++, Ruby, Delphy.

b.Sử dụng ActiveMQ như thế nào?

Cài đặt ActiveMQ. Với Windows thì chúng ta chỉ việc download file .zar về bung file với chạy. Với Lunix thì những chúng ta cũng có thể xem thêm cài đặt theo link: http://activemq.apabít.org/getting-started.html- Viết ứng dụng gửi tin Producer.Dưới đây là source code dễ dàng sản xuất cùng gửi thực hiện gửi tin từng giây một lượt.

Xem thêm: Prototyping Là Gì ? Tầm Quan Trọng Của Lập Trình Hướng Đối Tượng (Oop)

import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apađậy.activemq.ActiveMQConnectionFactory; publicclass ProducerMsging //Địa chỉ lắp thêm thiết lập ActiveMQ cùng với cổng khoác định là 61616 privatestatic String url = “failover://tcp://192.168.1.218:61616″; //Tên Topic privatestatic String subject = “TESTQUEUE1″; publicstaticvoid main(String<> args) throws JMSException // Getting JMS connection from the server and starting it ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Connection connection = connectionFactory.createConnection(); connection.start(); // JMS messages are sent & received using a Session. We will // create here a non-transactional session object. If you want // lớn use transactions you should mix the first parameter khổng lồ ‘true’ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Destination represents here our queue ‘TESTQUEUE’ on the // JMS VPS. You don’t have khổng lồ bởi vì anything special on the // server khổng lồ create it, it will be created automatically. Destination destination = session.createQueue(subject); // MessageProducer is used for sending messages (as opposed // to MessageConsumer which is used for receiving them) MessageProducer producer = session.createProducer(destination); //Delay waiting consumer start try // We will send a small text message saying ‘Hello’ in Japanese int msgTemp = 0; while(true) msgTemp++; String msg = “TextMessage-” + String.valueOf(msgTemp); TextMessage message = session.createTextMessage(msg); // Here we are sending the message! producer.send(message); System.out.println(“‘” + msg + “‘ has been send.”); //Delay 1s Thread.sleep(1000); catch (InterruptedException e) System.out.println(“Error when during sent message: “+ e.printStackTrace()); - Viết vận dụng thừa nhận tin Consumer

Dưới đó là source code phần dìm tin

import javax.jms.Connection; Import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apabịt.activemq.ActiveMQConnectionFactory; publicclass ConsumerMsging // URL of the JMS VPS privatestatic String url = “failover://tcp://192.168.1.218:61616″; // Name of the queue we will receive messages from privatestatic String subject = “TESTQUEUE1″; publicstaticvoid main(String<> args) throws JMSException // Getting JMS connection from the server ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Connection connection = connectionFactory.createConnection(); connection.start(); // Creating session for seding messages Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Getting the queue ‘TESTQUEUE’ Destination destination = session.createQueue(subject); // MessageConsumer is used for receiving (consuming) messages MessageConsumer consumer = session.createConsumer(destination); while(true) Message message = consumer.receive(); // There are many types of Message & TextMessage // is just one of them. Producer sent us a TextMessage // so we must cast khổng lồ it lớn get access to lớn its .getText() // method. if (message instanceof TextMessage) TextMessage textMessage = (TextMessage) message; System.out.println(“Received message ‘” + textMessage.getText() + “‘”); //connection.close(); c. Làm sao giám sát gửi nhấn tin trong ActiveMQActiveMQ cung ứng bối cảnh website thân thiện để làm chủ tin.