رسالة قائمة انتظار الوسيطة هو نظام الموزعة، عنصرا هاما، أساسا لحل تطبيق إلى جانب والرسائل غير المتزامنة، وقطع حركة المرور أمام وغيرها من القضايا. عالية الأداء، وتوافر عالية، بنية قابلة والاتساق في نهاية المطاف، ونظم واسع النطاق الموزعة هو الوسيطة الأساسية. حاليا استخدام المزيد من طوابير رسالة لها ActiveMQ، RabbitMQ، كافكا، RocketMQ، MetaMQ وهلم جرا. يوفر springboot الدعم لنظام الدائرة، springboot يمكن بسهولة دمج هذه الرسائل الوسيطة.
للرسائل غير متزامن سيكون فئتين في التطبيق العملي:
الدائرة: تحفة هي ActiveMQ، ولكن أدائها ليست عالية، لأنه يتم تطبيق باستخدام برنامج جافا.
AMQP: مباشر المكون الرسالة استخدام البروتوكول، وهو ممثل الكتلة كما RabbitMQ، كممثل عالية الأداء كافكا.
1، والمشروع الجديد، المقابلة لpom.xml الوثائق التالية
< XMLNS مشروع = " XMLNS: XSI = " XSI: schemaLocation = "HTTP: // maven.apache.org/POM/4.0.0 " > < modelVersion > 4.0.0 < / ModelVersion > < groupId > الربيع سحابة < / GroupId > < artifactId > SC-activeMQ < / ArtifactId > < نص > 0.0.1 لقطة < / النسخة > < التعبئة والتغليف > جرة < / تغليف > < اسم > SC-activeMQ < / اسم > < رابط > < / URL > < أصل > < groupId > org.springframework.boot < / GroupId > < artifactId > الربيع التمهيد بداية الوالدين < / ArtifactId > < نص > 2.0.4.RELEASE < / النسخة > < / الرئيسي > < dependencyManagement > < تبعيات > < التبعية > < groupId > org.springframework.cloud < / GroupId > < artifactId > الربيع سحابة تبعيات < / ArtifactId > < نص > Finchley.RELEASE < / النسخة > < نوع > بوم < / النوع > < مجال > استيراد < / نطاق > < / التبعية > < / التبعيات > < / DependencyManagement > < خصائص > < project.build.sourceEncoding > UTF-8 < /project.build.sourceEncoding > < maven.compiler.source > 1.8 < /maven.compiler.source > < maven.compiler.target > 1.8 < /maven.compiler.target > < / خصائص > < تبعيات > < التبعية > < groupId > org.springframework.boot < / GroupId > < artifactId > ربيع التمهيد-بداية-activemq < / ArtifactId > < / التبعية > < التبعية > < groupId > org.apache.activemq < / GroupId > < artifactId > activemq النواة < / ArtifactId > < نص > 5.7.0 < / النسخة > < / التبعية > < التبعية > < groupId > org.apache.activemq < / GroupId > < artifactId > activemq السباحة < / ArtifactId > < / التبعية > < التبعية > < groupId > org.springframework.boot < / GroupId > < artifactId > الربيع التمهيد بداية على شبكة الإنترنت < / ArtifactId > < / التبعية > < التبعية > < groupId > org.springframework.boot < / GroupId > < artifactId > الربيع التمهيد بداية اختبار < / ArtifactId > < مجال > اختبار < / نطاق > < / التبعية > < / التبعيات > < / مشروع >
2، springboot الجديدة الطبقة بدء التشغيل ActiveMqApplication.java
حزمة sc.activemq، org.springframework.boot.SpringApplication الاستيراد؛ org.springframework.boot.autoconfigure.SpringBootApplication الاستيراد؛SpringBootApplicationpubli cclass ActiveMqApplication {ساكنة العام باطلة الرئيسية (سلسلة وسائط) {SpringApplication.run (ActiveMqApplication.class، وسائط )؛}}
3، نيو الملف application.yml
الخادم: الميناء: 9080spring: appliction: اسم: SC-activemqactivemq: وسيط رابط: TCP: // المضيف المحلي: 61616in الذاكرة: trueuser: adminpassword: adminpool: تمكين: truemax-اتصالات: 50expiry-مهلة: 10000idle-مهلة: 30000jms: حانة على نطاق فرعي: كاذبة # activemq الافتراضي توفير وضع الانتظار، لاستخدام احتياجات وضع موضوع ليكون تكوين حانة شبه المجال إلى true
ملاحظة: من خلال activemq الافتراضي توفير وضع الانتظار، لاستخدام احتياجات وضع الموضوع ليتم تكوينه spring.jms.pub شبه المجال إلى true
4، المنتج المستهلك الجديد
حزمة sc.activemq.service.impl، javax.jms.Destination الاستيراد؛ org.springframework.beans.factory.annotation.Autowired الاستيراد؛ استيراد org.springframework.jms.core.JmsMessagingTemplate، org.springframework.stereotype.Service الاستيراد؛ استيراد sc.activemq.service.ProductService، تنفذServicepublicclass ProductServiceImpl ProductService {Autowiredprivate jmsMessagingTemplate jmsMessagingTemplate،Overridepublic sendMessage باطل (Destinationdestination، رسالة سلسلة) {jmsMessagingTemplate.convertAndSend (الوجهة، الرسالة)؛}}
5، والمستهلك رسالة جديدة
طابور الوضع:
حزمة sc.activemq.service.consumer، org.springframework.jms.annotation.JmsListener الاستيراد؛ org.springframework.stereotype.Component الاستيراد؛Componentpublicclass ConsumerQueue {// تكوين استخدام المستهلك رصد JmsListener الانتظار، حيث يتم تلقي النص رسالةJmsListener (الوجهة = "دائرة الخدمات الطبية المشتركة قائمة انتظار") receiveQueue الفراغ الجمهور (النص سلسلة) {System.out.println ( "ConsumerQueue تتلقى:" + النص)؛}}
نموذج الاشتراك:
حزمة sc.activemq.service.consumer، org.springframework.jms.annotation.JmsListener الاستيراد؛ org.springframework.stereotype.Component الاستيراد؛Componentpublicclass ConsumerTopic {// تكوين استخدام المستهلك رصد JmsListener الانتظار، حيث يتم تلقي النص رسالةJmsListener (الوجهة = "دائرة الخدمات الطبية المشتركة، موضوع") receiveQueue الفراغ الجمهور (النص سلسلة) {System.out.println ( "ConsumerTopic تتلقى:" + النص)؛}}
6، وفئة اختبار جديدة
حزمة sc.activemq؛ استيراد org.apache.activemq.command.ActiveMQQueue؛ استيراد org.apache.activemq.command.ActiveMQTopic؛ استيراد org.junit.Test؛ استيراد org.junit.runner.RunWith، org.springframework.beans الاستيراد. factory.annotation.Autowired؛ استيراد org.springframework.boot.test.context.SpringBootTest، org.springframework.test.context.junit4.SpringRunner الاستيراد؛ sc.activemq.service.ProductService الاستيراد؛RunWith (SpringRunner.class)SpringBootTestpublicclass ActiveMqTest {Autowiredprivate ActiveMQQueue طابور،Autowiredprivate ActiveMQTopic الموضوع؛Autowiredprivate productService productService، testJms الفراغTestpublic () {سلسلة msgQueue = "الذهب الإرسال"، ل(INTI = 0؛ ط < 5؛ ط ++) {productService.sendMessage (this.queue، msgQueue + ط)؛} سلسلة msgTopic = "فضة الإرسال"، ل(INTI = 0؛ ط < 5؛ ط ++) {productService.sendMessage (this.topic، msgTopic + ط)؛} {محاولة Thread.sleep (10000)؛} صيد (InterruptedException ه) {}}}
7 اختبار
يتم تسجيل ActiveMq منصة الإدارة: HTTP: // المضيف المحلي: 8161 /
انتظار الوضع:
(1) تكوين spring.jms.pub شبه المجال هو كاذبة
(2) لاحظ رموز الطبقة الاختبار التالي
(3) تشغيل فئة اختبار
نموذج الاشتراك:
(1) تكوين spring.jms.pub شبه المجال هو الصحيح
(2) لاحظ رموز الطبقة الاختبار التالي
(3) تشغيل فئة اختبار