Springboot 통합 Active 메시지 큐
10154 단어 Springboot통합Active메시지 큐
Active 는 Apache 회사 산하 메시지 버스 입 니 다.ActiveMQ 는 자바 Message Service(JMS)가 메 시 지 를 대상 으로 하 는 중간 부분 을 호 환 하 는 오픈 소스 입 니 다.느슨 한 결합 을 제공 하 는 응용 프로그램 구조 입 니 다.
주로 서비스 와 서비스 사이 에서 비동기 통신 을 하 는 데 쓰 인 다.
1.구축 절차
1、해당 jar 패키지
<!-- ActiveMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
2,application.properties 파일
# jms , ,
spring.activemq.broker-url=tcp://47.96.44.110:61616
spring.activemq.user=admin
spring.activemq.password=admin
#
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100
# ( )
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
# , / ( )
# spring.jms.pub-sub-domain=true
3.Springboot 주요 클래스
<!-- @EnableJms , , , -->
@SpringBootApplication
@EnableJms
4.5...............................................................2.점대 점 사례
나 는 이 사례 에서 두 개의 점 대 점 대기 열 을 만 들 었 기 때문에 그 는 두 개의 quue 대상 이 있 고 모든 quue 대상 에 대응 하 며 단일 하 게 대응 하 는 소비자 가 있 을 것 이다.
1.Springboot 주 클래스
@SpringBootApplication
@EnableJms
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
// Queue , sringboot , queue "first.queue".
@Bean
public Queue queue(){
return new ActiveMQQueue("first.queue");
}
}
2.1 first.queue 대응 소비자
@Component
public class FirstConsumer {
// "first.queue" , JmsListener ,
@JmsListener(destination="first.queue")
public void receiveQueue(String text){
System.out.println("FirstConsumer :"+text);
}
}
2.2.to.queue 는 소비자 에 대응 합 니 다(나중에 생 성 됩 니 다)
@Component
public class TwoConsumer {
// "two.queue"
@JmsListener(destination="two.queue")
public void receiveQueue(String text){
System.out.println("TwoConsumer :"+text);
}
}
3.서비스 류
/**
* :
*/
public interface ProducerService {
// : ,
public void sendMessage(Destination destination, final String message);
// : ,
public void sendMessage( final String message);
}
4.ServiceImpl 실현 류
/**
* :
*/
@Service
public class ProducerServiceImpl implements ProducerService{
// Springboot bean
@Autowired
private Queue queue;
// broker , JDBC
@Autowired
private JmsMessagingTemplate jmsTemplate;
// ,destination ,message
@Override
public void sendMessage(Destination destination, String message) {
jmsTemplate.convertAndSend(destination, message);
}
// ,queue ,message
@Override
public void sendMessage(final String message) {
jmsTemplate.convertAndSend(this.queue, message);
}
}
5.QueueController 클래스
/**
* :
*/
@RestController
@RequestMapping("/api/v1")
public class QueueController {
@Autowired
private ProducerService producerService;
// Springboot quene
@GetMapping("first")
public Object common(String msg){
producerService.sendMessage(msg);
return "Success";
}
// two.queue
@GetMapping("two")
public Object order(String msg){
Destination destination = new ActiveMQQueue("two.queue");
producerService.sendMessage(destination, msg);
return "Success";
}
}
6.사례 설명:시연 효과 에서 다음 과 같은 결론 을 얻 을 수 있다.
1:springboot 가 시 작 될 때 이 두 개의 대기 열 이 생 성 되 었 고 그들 은 모두 한 명의 소비자 가 있 을 것 이다.
2:내 가 페이지 를 통 해 방 문 했 을 때 생산자 가 메 시 지 를 대열 에 넣 는 것 과 같 습 니 다.넣 으 면 소비자 에 게 감청 되 고 생산자 가 넣 은 값 을 얻 고 배경 에서 출력 할 수 있 습 니 다.
페이지 의 네 단 어 를 설명 하 는 김 에:
Number Of Pending Messages:처리 할 메시지 의 수량 입 니 다.우 리 는 매번 감청 처 리 를 받 기 때문에 처리 해 야 할 것 이 없다.만약 존재 한다 면 이 안에 어디 가 고장 이 났 는 지 말 하고 조사 해 야 한다.
Number Of Consumers : 소비자 수
Messages Enqueued: 소식 배열,이것 은 늘 어 날 뿐 보이 지 않 는 다.이미 얼마나 많은 소식 을 처 리 했 는 지 를 대표 한다.
Messages Dequeued: 소식 이 나오다.
3.게시/구독 자 모드
위의 점 대 점 코드 를 바탕 으로 게시/구독 관련 코드 를 추가 합 니 다.
1.application.properties 파일
# , / ( )
spring.jms.pub-sub-domain=true
2.Springboot 주 클래스 추가
// topic
@Bean
public Topic topic(){
return new ActiveMQTopic("video.topic");
}
3.여러 소비자 류 추가
//
@Component
public class TopicSub {
@JmsListener(destination="video.topic")
public void receive1(String text){
System.out.println("video.topic :receive1="+text);
}
@JmsListener(destination="video.topic")
public void receive2(String text){
System.out.println("video.topic :receive2="+text);
}
@JmsListener(destination="video.topic")
public void receive3(String text){
System.out.println("video.topic :receive3="+text);
}
}
4.서비스 클래스
// :
public void publish(String msg);
5.ServiceImpl 실현 클래스
//======= =========
@Autowired
private Topic topic;
@Override
public void publish(String msg) {
this.jmsTemplate.convertAndSend(this.topic, msg);
}
6.Controller 클래스
// two.queue
@GetMapping("topic")
public Object topic(String msg){
producerService.publish(msg);
return "Success";
}
7.데모 효과:프 리 젠 테 이 션 효과 요약 은 다음 과 같 습 니 다.
1:Springboot 가 시 작 될 때 Topics 디 렉 터 리 에 모두 5 명의 소비자 가 나 타 났 습 니 다.first.queue 한 소비자,to.queue 한 소비자,video.topic 세 소비자
2:콘 솔 에 정 보 를 입력 하면 video.topic 의 세 소비자 가 video.topic 에서 발표 한 정 보 를 감청 하고 콘 솔 에서 인쇄 합 니 다.
4.점 대 점 과 구독 을 동시에 유효 하 게 하 는 방법
왜 그 랬 을 까?내 가 위 를 향 해 똑 같이 동시에 열 면 점 대 점 모드 가 이미 효력 을 잃 었 다 는 것 을 알 수 있 기 때문이다.
효과 시범
시범 효과 에서 다음 과 같은 결론 을 얻 을 수 있다.
1:우 리 는 우리 가 페이지 에.../two?msg=555 메 시 지 를 입력 한 후에 배경 에서 메 시 지 를 인쇄 하 는 데 성공 하지 못 한 것 을 발견 했다.Active 인터페이스 를 보면 이 quue 대상 은 처리 해 야 할 소식 이 있 지만 이에 대응 하 는 소비자 수 는 0 이라는 것 을 알 수 있 습 니 다.
2:그러나 우 리 는 topic 페이지 를 열 었 을 때 여기에 소비자 가 존재 한 다 는 것 을 발견 했다.
그래서 저 는 개인 적 으로 동시 작 동 할 때 발생 하 는 소비자 들 은 기본적으로 Topic 소비자 이 고 Queue 소비자 가 없 기 때문에 queue 가 처리 해 야 할 정 보 를 듣 지 못 한 다 는 것 을 이해 합 니 다.
설정 파일 이 추가 되 지 않 을 때:spring.jms.pub-sub-domain=true 그러면 시스템 은 기본적으로 quene(점 대 점 모드)를 지원 하지만 이 설정 을 추가 하면 시스템 은 게시 구독 만 지원 하 는 모드 가 됩 니 다.
그러면 어떻게 동시에 성공 할 수 있 습 니까?
사고방식 은 다음 과 같다.
첫 번 째 단계:설정 파일 의 것 을 제거 해 야 합 니 다:
# , / ( )
#spring.jms.pub-sub-domain=true
두 번 째 단계:구독 자 를 발표 하 는 중 소비자 에 게 독립 된 container Factory 를 지정 합 니 다.위의 설정 을 제거 하면 시스템 은 기본적으로 queue 이기 때문에@JmsListener 는 독립 된 container Factory 를 지정 하지 않 으 면 queue 메시지 만 소비 할 수 있 습 니 다.
@JmsListener(destination="video.topic", containerFactory="jmsListenerContainerTopic")
public void receive1(String text){
System.out.println("video.topic :receive1="+text);
}
@JmsListener(destination="video.topic", containerFactory="jmsListenerContainerTopic")
public void receive2(String text){
System.out.println("video.topic :receive2="+text);
}
// containerFactory="jmsListenerContainerTopic"
@JmsListener(destination="video.topic")
public void receive3(String text){
System.out.println("video.topic :receive3="+text);
}
세 번 째 단계:독립 된 topic 정의 독립 된 JmsListener Containerspringboot 메 인 클래스 에 추가:
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
효과:결론 을 얻다.
1:점 대 점,구독 발표 모두 유용
2:receive 3 은 독립 된 container Factory 를 지정 하지 않 은 것 처럼 인쇄 되 지 않 았 습 니 다.
소스 코드
github 주소:https://github.com/yudiandemingzi/springbootAcitveMQ
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.