ActiveMq学習ノート

7499 ワード

まずlocalhost:8161 web操作ページの小さな翻訳をします
原版
ActiveMq学习笔记_第1张图片
翻訳
ActiveMq学习笔记_第2张图片
私たちはhttp://localhost:8161管理ページにアクセスし、tcp://localhost:61616メッセージ・サーバに接続します.使用するユーザー名とパスワードは次のファイルにあります(デフォルトはadmin=admin)
私はSpringBootフレームワークを使っています.次はアプリケーションです.ymlの構成情報
server:
  port: 39527
  
spring:
  jms:
    pub-sub-domain: false #       ,    true   topic  ,   false  queue    
  activemq:
    user: admin
    password: admin
    broker-url: tcp://localhost:61616 #           

メッセージキューには、生産者/製造者/Producerと消費者/受信者/consumerの2つのモードが必要です.1つはqueueポイント・ツー・ポイント・メッセージ・モードです.
もう1つはTopic購読者モードです.
次にqueueメッセージモード基本コードの例を示します.
Producer
@Service
public class MessageProduceImpl{

	@Autowired
	private JmsMessagingTemplate jmsmessagingtemplate;
	
	
	public void sendMessage(String msg) {
		// TODO Auto-generated method stub
		//           ,            :eclipse
		ActiveMQQueue activeMQQueue = new ActiveMQQueue("eclipse");
		this.jmsmessagingtemplate.convertAndSend(activeMQQueue,msg);
	}

}

JmsMessagingTemplateは独自のメッセージモデルで、springbootは簡単です.それから2つのパラメータをJmsMessagingTemplateに渡せばいいです.
いくつかのパラメータを設定して実行すると、webインタフェースはこうなります.
ActiveMq学习笔记_第3张图片
eclipseという名前のポイントが表示されます.さっき自分で200個のデータをテストしましたが、受信ポイントが設定されていません.だから、彼のNumber Of Pending Messagesは200個のデータが処理されていないことを表示します.
次は消費者です
Comsumer
@Service
public class MessageConsumerService {
	@JmsListener(destination = "eclipse")
	public void receiveMessage(String text){
		
		System.out.println("------------    ------------"+text);
	}
	
}

メソッドに注記@JmsListener(destination=「eclipse」)を付けるだけでOK
消費者のみが実行され、受信ポイントが「eclipse」である場合、実行すると、以前にポイントeclipseに格納されていた200件のデータが受信されます.
ActiveMq学习笔记_第4张图片
次はメッセージを送信する小さなテストクラスです.
package com.cn.sola;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.cn.sola.producer.MessageProduceImpl;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqApplicationTests {

	@Autowired
	private MessageProduceImpl messagerproducerservice;
	
	@Test
	public void contextLoads() {
		
		for (int i = 0; i < 200; i++) {
			
			messagerproducerservice.sendMessage(i+"");
		}
	}

}

Queue基本小例終了
基本サブスクライバモードの例
サブスクライバモードはポイントを作成し、多くの人がここに物を取りに来て、一人一人が自分の標識を持っています.
application.ymlかそれともどのymlかは変わらない
まず1つの構成クラス
package com.cn.sola.config;

import javax.jms.ConnectionFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;

@Configuration
public class config {

	
	@Bean
    JmsListenerContainerFactory> myJmsContainerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
}

Topicはメッセージクラスを発表する
Publisher
package com.cn.sola.topic.publisher;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
@Service
public class Publisher {
	
	@Autowired
	private JmsMessagingTemplate jmssagingtemplate;
	
	public void publish(String destinationName,String message){
		
		ActiveMQTopic activeMQTopic = new ActiveMQTopic(destinationName);
		
		System.out.println("================>>>  topic    "+message);
		
		this.jmssagingtemplate.convertAndSend(activeMQTopic,message);
	}
}

クラスオブジェクトがnew ActiveMQTopicになった他に何の変化もない
Topic受信メッセージクラス
私は暇にいくつかを建てました.
Subscriber
package com.cn.sola.topic.subscriber;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class Subscriber {

	@JmsListener(destination = "topic.9981",containerFactory = "myJmsContainerFactory")
	public void subscribeone(String text){
		
		System.out.println("===============<
package com.cn.sola.topic.subscriber;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

@Service
public class SubscriberCopy {

	@JmsListener(destination = "topic.9981",containerFactory = "myJmsContainerFactory")
	public void subscribetwo(String text){
		
		System.out.println("===============<
package com.cn.sola.topic.subscriber;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class SubscriberCopytwo {

	@JmsListener(destination = "topic.9981",containerFactory = "myJmsContainerFactory")
	public void subscribethree(String text){
		
		System.out.println("===============<

テストクラス
package com.cn.sola;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.cn.sola.producer.MessageProduceImpl;
import com.cn.sola.topic.publisher.Publisher;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqApplicationTests {

	@Autowired
	private MessageProduceImpl messagerproducerservice;
	
	@Autowired
	private Publisher publisher;
	
	@Test
	public void contextLoads() {
		
		publisher.publish("topic.9981", "topic.9981    ");
		publisher.publish("topic.9981", "topic.9981    ");
		publisher.publish("topic.9981", "topic.9981    ");
	}

}
に対応する消費者は、すべてテスト情報を受信することができる.