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中文网其他相关文章!