Rumah > Java > javaTutorial > teks badan

Lihat dengan pantas cara Spring Boot menyokong JMS

DDD
Lepaskan: 2024-11-02 10:30:03
asal
313 orang telah melayarinya

Quick look on how Spring Boot supports JMS

Contoh kod

Kod contoh adalah dari sini dengan beberapa pengubahsuaian. Semasa menulis Spring Boot 3.3.0 (Spring Framework 6.1.8) digunakan.

lengkap/pom.xml

Tukar kepada broker terbenam 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>

<!-- ... -->
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

lengkap/src/main/resources/application.properties

Aktifkan pengelogan nyahpepijat dan sediakan url broker terbenam

#spring.artemis.mode=embedded
debug=true
spring.activemq.broker-url=vm://localhost?broker.persistent=false
Salin selepas log masuk
Salin selepas log masuk

lengkap/src/main/java/hello/Application.java

Gunakan JmsListenerContainerFactory kacang yang dicipta oleh Spring Boot daripada dibina oleh kami sendiri

@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;
    }*/

    //...

}
Salin selepas log masuk

lengkap/src/main/java/hello/Receiver.java

Tentukan lalai JmsListenerContainerFactory

@Component
public class Receiver {

    //@JmsListener(destination = "mailbox", containerFactory = "myFactory")
    @JmsListener(destination = "mailbox")
    public void receiveMessage(Email email) {
        System.out.println("Received <" + email + ">");
    }

}
Salin selepas log masuk

Log konfigurasi automatik Spring Boot

Hanya konfigurasi berkaitan JMS ditunjukkan.

   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)
Salin selepas log masuk

Antara muka yang berkaitan

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

Mengenai ConnectionFactory

Pelaksanaan ActiveMQ ialah org.apache.activemq.ActiveMQConnectionFactory, tetapi Spring Framework tidak menggunakannya secara langsung. Kelas dibalut oleh org.springframework.jms.connection.CachingConnectionFactory untuk mengikuti

  • hanya satu Sambungan dibuat dan ini akan digunakan semula
  • cache MessageProducer dan MessageConsumer (Dalam contoh ini hanya MessageProducer dicache)

Penerbit

Menghantar mesej melalui 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>

<!-- ... -->
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

JmsTemplate bean dibina oleh org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration.JmsTemplateConfiguration#jmsTemplate. Kacang MessageConverter diperlukan untuk menyahsiri DTO.

DestinationResolver yang digunakan ialah org.springframework.jms.support.destination.DynamicDestinationResolver, kelas hanya mendapatkan contoh jakarta.jms.Destination dengan memanggil 🎜>jakarta.jms.Session#createTopic atau jakarta.jms.Session#createQueue.

pelanggan

org.springframework.jms.annotation.JmsListener anotasi

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

org.springframework.jms.listener.DefaultMessageListenerContainer kelas

Dalam spesifikasi JMS, pemprosesan mesej tak segerak disokong dan pendengar dijalankan di bawah urutan pembekal 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>

<!-- ... -->
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

Tetapi pendekatan tak segerak tidak digunakan dalam Rangka Kerja Spring, API segerak (pungutan suara) digunakan. Kod sebenar terdapat dalam org.springframework.jms.support.destination.JmsDestinationAccessor#receiveFromConsumer.

#spring.artemis.mode=embedded
debug=true
spring.activemq.broker-url=vm://localhost?broker.persistent=false
Salin selepas log masuk
Salin selepas log masuk
Kelas

org.springframework.jms.listener.DefaultMessageListenerContainer.AsyncMessageListenerInvoker adalah untuk melaksanakan tugas tinjauan pendapat secara berkala. Ini dijadualkan dalam org.springframework.core.task.SimpleAsyncTaskExecutor.

Satu contoh DefaultMessageListenerContainer dibuat untuk satu @JmsListener fungsi beranotasi. Ini dihasilkan oleh org.springframework.jms.config.DefaultJmsListenerContainerFactory.

Sudah tentu contoh MessageConverter dan ExceptionListener diperlukan.

Atas ialah kandungan terperinci Lihat dengan pantas cara Spring Boot menyokong JMS. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan