首頁 >Java >Spring Boot 嵌入式 ActiveMQ Artemis 代理連接

Spring Boot 嵌入式 ActiveMQ Artemis 代理連接

WBOY
WBOY轉載
2024-02-11 13:39:08919瀏覽

php小編香蕉為您介紹Spring Boot嵌入式ActiveMQ Artemis代理連接。 ActiveMQ Artemis是一款高效能、可擴展的訊息代理,常用於建立可靠的訊息傳遞系統。 Spring Boot提供了簡單且方便的方式來整合ActiveMQ Artemis。透過嵌入式連接,我們可以在Spring Boot應用中直接使用ActiveMQ Artemis,無需額外的配置和部署。這種連接方式不僅簡化了開發過程,還提供了更高的效能和可靠性,使得訊息傳遞更加高效和穩定。無論是開發即時應用、訊息佇列、或非同步通信,Spring Boot嵌入式ActiveMQ Artemis代理連線都是一個值得探索的選擇。

問題內容

我有一個非常簡單的 spring boot 2.7.6 activemq artemis 應用程序,用於偵聽訊息。

package hello;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.bean;
import org.springframework.jms.annotation.jmslistener;
import org.springframework.jms.annotation.enablejms;
import org.springframework.jms.config.defaultjmslistenercontainerfactory;
import org.springframework.boot.autoconfigure.jms.defaultjmslistenercontainerfactoryconfigurer;
import javax.jms.connectionfactory;
import org.springframework.jms.config.jmslistenercontainerfactory;

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

    public static void main(string[] args) {
       springapplication.run(application.class, args);
    }

    @jmslistener(destination = "my-queue-1")
    public void listen(string in) {
       system.out.println(in);
    }
}

這是配置嵌入式代理程式的程式碼。我只是透過添加多個接受器來猜測。不同的帖子引用了 addconnectorconfiguration,但到目前為止它們似乎都不起作用。

package hello;

import org.apache.activemq.artemis.api.core.transportconfiguration;
import org.apache.activemq.artemis.core.remoting.impl.netty.nettyacceptorfactory;
import org.apache.activemq.artemis.core.remoting.impl.netty.nettyconnectorfactory;
import org.springframework.boot.autoconfigure.jms.artemis.artemisconfigurationcustomizer;
import org.springframework.context.annotation.configuration;


@configuration
public class artemisconfig implements artemisconfigurationcustomizer {
    @override
    public void customize(org.apache.activemq.artemis.core.config.configuration configuration) {
      configuration.addacceptorconfiguration("remote", "tcp://0.0.0.0:61616");
    }
}

透過這個簡單的 application.properties

spring.artemis.mode=embedded
spring.artemis.embedded.server-id=54321
spring.artemis.embedded.queues=my-queue-1
spring.artemis.embedded.enabled=true

然後我有另一個 spring boot 應用程序,它會產生訊息並將它們發送到代理地址。

package broker.producer;

import org.apache.activemq.artemis.jms.client.activemqconnectionfactory;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.bean;
import org.springframework.jms.core.jmstemplate;
import org.springframework.jms.support.destination.jndidestinationresolver;
import org.springframework.stereotype.service;

@service
public class jmsproducer {

  @value("${spring.jms.template.default-destination}")
  private string defaultdestination;

  logger log = loggerfactory.getlogger(jmsproducer.class);

  @bean
  public activemqconnectionfactory activemqconnectionfactory() {
      activemqconnectionfactory activemqconnectionfactory = new activemqconnectionfactory("tcp://localhost:61616");

      return activemqconnectionfactory;
  }

  @bean
  public jndidestinationresolver jndidestinationresolver() {
    return new jndidestinationresolver();
  }

  @bean
  public jmstemplate jmstemplate() {
    jmstemplate template = new jmstemplate();
    template.setconnectionfactory(activemqconnectionfactory());
    template.setpubsubdomain(false); // false for a queue, true for a topic
    template.setdefaultdestinationname(defaultdestination);

    return template;
  }


  public void send(string message) {
    jmstemplate jmstemplate = jmstemplate();

    log.info("sending message='{}'", message);
    jmstemplate.convertandsend(message);
    log.info("sent message='{}'", message);
  }
}

然後我啟動每個應用程式並嘗試呼叫發送方法,但由於此錯誤,我無法從生產者應用程式連接到代理:

2024-01-16 10:25:00.596 ERROR 30486 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jms.UncategorizedJmsException: Uncategorized exception occurred during JMS processing; nested exception is javax.jms.JMSException: Failed to create session factory; nested exception is ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ219007: Cannot connect to server(s). Tried with all available servers.]] with root cause

生產者應用程式能夠很好地連接到 activemq artemis 的 docker 實例。

目前,兩個應用程式都在同一台電腦上運行,但在產品中,我希望每個應用程式在單獨的 pod 中運行。

解決方法

我整理了一個非常簡單的專案作為概念驗證,以確保您正在做的事情是可能的,並且一切對我來說都運作良好。嵌入式代理程式已啟動並在連接埠 61616 上接受來自遠端客戶端的連線。

這是 application.java

package hello;

import javax.jms.connectionfactory;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.autoconfigure.jms.defaultjmslistenercontainerfactoryconfigurer;
import org.springframework.context.annotation.bean;
import org.springframework.jms.annotation.enablejms;
import org.springframework.jms.annotation.jmslistener;
import org.springframework.jms.config.defaultjmslistenercontainerfactory;
import org.springframework.jms.config.jmslistenercontainerfactory;

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

    public static void main(string[] args) {
        springapplication.run(application.class, args);
    }

    @jmslistener(destination = "my-queue-1")
    public void listen(string in) {
        system.out.println(in);
    }
}

這是 artemisconfig.java

package hello;

import org.springframework.boot.autoconfigure.jms.artemis.artemisconfigurationcustomizer;
import org.springframework.context.annotation.configuration;


@configuration
public class artemisconfig implements artemisconfigurationcustomizer {
    @override
    public void customize(org.apache.activemq.artemis.core.config.configuration configuration) {
       try {
          configuration.addacceptorconfiguration("remote", "tcp://0.0.0.0:61616");
       } catch (exception e) {
          e.printstacktrace();
       }
    }
}

這是我的 application.properties

spring.artemis.mode=embedded
spring.artemis.embedded.server-id=54321
spring.artemis.embedded.queues=my-queue-1
spring.artemis.embedded.enabled=true

最後,這是我的 pom.xml

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelversion>4.0.0</modelversion>
   <parent>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-parent</artifactid>
      <version>2.7.6</version>
      <relativepath/>
   </parent>
   <groupid>com.example</groupid>
   <artifactid>spring-boot-customized-activemq-artemis</artifactid>
   <version>0.0.1-snapshot</version>
   <properties>
      <java.version>17</java.version>
      <spring.version>2.7.6</spring.version>
      <activemq.artemis.version>2.19.1</activemq.artemis.version>
   </properties>

   <dependencies>
      <dependency>
         <groupid>org.springframework.boot</groupid>
         <artifactid>spring-boot-starter-artemis</artifactid>
      </dependency>
      <dependency>
         <groupid>org.apache.activemq</groupid>
         <artifactid>artemis-jms-server</artifactid>
     <version>${activemq.artemis.version}</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-maven-plugin</artifactid>
        <version>${spring.version}</version>
         </plugin>
      </plugins>
   </build>
</project>

我像這樣啟動應用程式:

mvn spring-boot:run

我看到這樣的日誌記錄:

...
... AMQ221020: Started EPOLL Acceptor at 0.0.0.0:61616 for protocols [CORE]
... AMQ221007: Server is now live
...

我可以從另一個應用程式向代理程式發送訊息,並且 jmslistener 會接收該訊息。

我已將專案上傳到 github

以上是Spring Boot 嵌入式 ActiveMQ Artemis 代理連接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除