예제 코드는 일부 수정된 것입니다. 작성 시점에서는 Spring Boot 3.3.0(Spring Framework 6.1.8)을 사용하고 있습니다.
완전/pom.xml
ActiveMQ 임베디드 브로커로 전환
<dependency> <groupId>org.springframework.boot</groupId> <!--<artifactId>spring-boot-starter-artemis</artifactId>--> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <!--<artifactId>artemis-jakarta-server</artifactId>--> <artifactId>activemq-broker</artifactId> <scope>runtime</scope> </dependency> <!-- ... -->
완전/src/main/resources/application.properties
디버그 로깅을 켜고 내장된 브로커 URL을 설정하세요
#spring.artemis.mode=embedded debug=true spring.activemq.broker-url=vm://localhost?broker.persistent=false
완전/src/main/java/hello/Application.java
직접 빌드하는 대신 Spring Boot에서 생성한 JmsListenerContainerFactory Bean을 사용하세요
@SpringBootApplication @EnableJms public class Application { /*@Bean public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); // This provides all auto-configured defaults to this factory, including the message converter configurer.configure(factory, connectionFactory); // You could still override some settings if necessary. return factory; }*/ //... }
완전/src/main/java/hello/Receiver.java
기본값 지정 JmsListenerContainerFactory
@Component public class Receiver { //@JmsListener(destination = "mailbox", containerFactory = "myFactory") @JmsListener(destination = "mailbox") public void receiveMessage(Email email) { System.out.println("Received <" + email + ">"); } }
JMS 관련 구성만 표시됩니다.
ActiveMQAutoConfiguration matched: - @ConditionalOnClass found required classes 'jakarta.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition) - @ConditionalOnMissingBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition) ActiveMQAutoConfiguration#activemqConnectionDetails matched: - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionDetails; SearchStrategy: all) did not find any beans (OnBeanCondition) ActiveMQConnectionFactoryConfiguration matched: - @ConditionalOnMissingBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition) ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration matched: - @ConditionalOnProperty (spring.activemq.pool.enabled=false) matched (OnPropertyCondition) ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration.CachingConnectionFactoryConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.jms.connection.CachingConnectionFactory' (OnClassCondition) - @ConditionalOnProperty (spring.jms.cache.enabled=true) matched (OnPropertyCondition) JmsAnnotationDrivenConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.jms.annotation.EnableJms' (OnClassCondition) JmsAnnotationDrivenConfiguration#jmsListenerContainerFactory matched: - @ConditionalOnSingleCandidate (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found a single bean 'jmsConnectionFactory'; @ConditionalOnMissingBean (names: jmsListenerContainerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition) JmsAnnotationDrivenConfiguration#jmsListenerContainerFactoryConfigurer matched: - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; SearchStrategy: all) did not find any beans (OnBeanCondition) JmsAutoConfiguration matched: - @ConditionalOnClass found required classes 'jakarta.jms.Message', 'org.springframework.jms.core.JmsTemplate' (OnClassCondition) - @ConditionalOnBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found bean 'jmsConnectionFactory' (OnBeanCondition) JmsAutoConfiguration.JmsTemplateConfiguration#jmsTemplate matched: - @ConditionalOnSingleCandidate (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found a single bean 'jmsConnectionFactory'; @ConditionalOnMissingBean (types: org.springframework.jms.core.JmsOperations; SearchStrategy: all) did not find any beans (OnBeanCondition) JmsAutoConfiguration.MessagingTemplateConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.jms.core.JmsMessagingTemplate' (OnClassCondition) JmsAutoConfiguration.MessagingTemplateConfiguration#jmsMessagingTemplate matched: - @ConditionalOnSingleCandidate (types: org.springframework.jms.core.JmsTemplate; SearchStrategy: all) found a single bean 'jmsTemplate'; @ConditionalOnMissingBean (types: org.springframework.jms.core.JmsMessageOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)
Interface | Function |
---|---|
org.springframework.jms.support.destination.DestinationResolver | lookup jakarta.jms.Destination instance by String name |
org.springframework.transaction.jta.JtaTransactionManager | control transaction by JTA |
org.springframework.jms.support.converter.MessageConverter | serialize/deserialize DTO instance |
jakarta.jms.ExceptionListener | processor when jakarta.jms.JMSException throws. One implementation is SingleConnectionFactory, connection managed by that class will be restarted once exception is catched |
io.micrometer.observation.ObservationRegistry | for statistics |
ActiveMQ 구현체는 org.apache.activemq.ActiveMQConnectionFactory이지만 Spring Framework에서는 이를 직접 사용하지 않습니다. 클래스는 다음을 위해 org.springframework.jms.connection.CachingConnectionFactory로 래핑됩니다
org.springframework.jms.core.JmsTemplate을 통해 메시지 보내기
<dependency> <groupId>org.springframework.boot</groupId> <!--<artifactId>spring-boot-starter-artemis</artifactId>--> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <!--<artifactId>artemis-jakarta-server</artifactId>--> <artifactId>activemq-broker</artifactId> <scope>runtime</scope> </dependency> <!-- ... -->
JmsTemplate 빈은 org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration.JmsTemplateConfiguration#jmsTemplate에 의해 빌드됩니다. DTO를 역직렬화하려면 MessageConverter Bean이 필요합니다.
DestinationResolver는 org.springframework.jms.support.destination.DynamicDestinationResolver이고, 클래스는 jakarta.jms.Destination 인스턴스를 호출하여 가져옵니다. 🎜>jakarta.jms.Session#createTopic 또는 jakarta.jms.Session#createQueue.
attribute | Function |
---|---|
id | prefix of thread name which run listener |
containerFactory | bean name of JmsListenerContainerFactory instance |
destination | the destination name for this listener |
subscription | the name of the durable subscription, if any |
selector | an optional message selector for this listener |
concurrency | number of thread running listener |
JMS 사양에서는 비동기 메시지 처리가 지원되며 리스너는 JMS 공급자의 스레드에서 실행됩니다.
<dependency> <groupId>org.springframework.boot</groupId> <!--<artifactId>spring-boot-starter-artemis</artifactId>--> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <!--<artifactId>artemis-jakarta-server</artifactId>--> <artifactId>activemq-broker</artifactId> <scope>runtime</scope> </dependency> <!-- ... -->
그러나 Spring Framework에서는 비동기식 접근 방식을 사용하지 않고 동기식 API(폴링)를 사용합니다. 실제 코드는 org.springframework.jms.support.destination.JmsDestinationAccessor#receiveFromConsumer에 있습니다.
#spring.artemis.mode=embedded debug=true spring.activemq.broker-url=vm://localhost?broker.persistent=false
org.springframework.jms.listener.DefaultMessageListenerContainer.AsyncMessageListenerInvoker 클래스는 주기적으로 폴링 작업을 수행하기 위한 클래스입니다. 이는 org.springframework.core.task.SimpleAsyncTaskExecutor에 예약되어 있습니다.
하나의 @JmsListener 주석이 달린 함수에 대해 하나의 DefaultMessageListenerContainer 인스턴스가 생성됩니다. 이는 org.springframework.jms.config.DefaultJmsListenerContainerFactory에서 생성됩니다.
물론 MessageConverter와 ExceptionListener 인스턴스가 필요합니다.
위 내용은 Spring Boot가 JMS를 지원하는 방법에 대한 간략한 살펴보기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!