我正在遵循 Introducing Spring Framework 6' 中的示例,特别是 chapter 9,其中我无法成功运行有关 JMS 的示例。
以下设置:
(主课)
@EnableJpaRepositories
@EnableJms
@SpringBootApplication
public class MyDocsApp {
public static void main(String[] args) {
SpringApplication.run(MyDocsApp.class, args);
}
@Bean
CommandLineRunner send(JmsPublisher publisher) {
return args -> {
IntStream.rangeClosed(1, 20).forEach(
(value) -> {
var uuid = UUID.randomUUID().toString();
publisher.send(
new Document(
"document " + uuid,
"/documents/doc" + uuid
));
}
);
};
}
出版商
@Component
public class JmsPublisher {
private final JmsTemplate jmsTemplate;
public JmsPublisher(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void send(Document document) {
jmsTemplate.send("documents", session -> session.createObjectMessage(document));
}
}
document
班级
@Entity
@Table(name="document")
public class Document implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Integer id;
@Column(nullable = false)
String name;
@Column(nullable = false)
String location;
Document() {
}
Document(String name, String location) {
setName(name);
setLocation(location);
}
// getter/setter omitted
gradle 文件。
plugins {
id "application"
id 'org.springframework.boot' version '3.0.6'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
}
dependencies {
implementation \
'org.springframework.boot:spring-boot-starter-data-jpa:3.0.6'
implementation \
'org.springframework.boot:spring-boot-starter-artemis:3.0.6'
implementation \
'com.h2database:h2:2.1.214'
// implementation \
// 'org.apache.activemq:artemis-jms-server:2.24.0'
}
application {
mainClass.set("md.MyDocsApp")
}
我现在的问题是我看不到需要在哪里启动 JMS 服务器。我需要做什么才能摆脱
Caused by: org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException: AMQ219007: Cannot connect to server(s). Tried with all available servers.
at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:703) ~[artemis-core-client-2.26.0.jar:2.26.0]
带有日志输出的完整堆栈跟踪如下:
2023-04-24T09:27:11.046+02:00 INFO 22314 --- [ main] md.MyDocsApp : No active profile set, falling back to 1 default profile: "default"
2023-04-24T09:27:11.268+02:00 INFO 22314 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-04-24T09:27:11.296+02:00 INFO 22314 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 22 ms. Found 1 JPA repository interfaces.
2023-04-24T09:27:11.550+02:00 INFO 22314 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-04-24T09:27:11.578+02:00 INFO 22314 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.1.7.Final
2023-04-24T09:27:11.744+02:00 INFO 22314 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-04-24T09:27:11.852+02:00 INFO 22314 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:7ef74512-9dd7-4cc1-9e98-e4c392d01864 user=SA
2023-04-24T09:27:11.853+02:00 INFO 22314 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-04-24T09:27:11.874+02:00 INFO 22314 --- [ main] SQL dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2023-04-24T09:27:12.312+02:00 INFO 22314 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-04-24T09:27:12.317+02:00 INFO 22314 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-04-24T09:27:12.767+02:00 INFO 22314 --- [ main] o.s.j.c.CachingConnectionFactory : Encountered a JMSException - resetting the underlying JMS Connection
jakarta.jms.JMSException: Failed to create session factory
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:867) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:284) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:279) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:427) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:352) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:327) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:242) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:196) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.AbstractJmsListeningContainer.createSharedConnection(AbstractJmsListeningContainer.java:412) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.AbstractJmsListeningContainer.establishSharedConnection(AbstractJmsListeningContainer.java:380) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.DefaultMessageListenerContainer.establishSharedConnection(DefaultMessageListenerContainer.java:861) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.AbstractJmsListeningContainer.doStart(AbstractJmsListeningContainer.java:292) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.AbstractJmsListeningContainer.start(AbstractJmsListeningContainer.java:277) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.listener.DefaultMessageListenerContainer.start(DefaultMessageListenerContainer.java:655) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.config.JmsListenerEndpointRegistry.startIfNecessary(JmsListenerEndpointRegistry.java:242) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.config.JmsListenerEndpointRegistry.start(JmsListenerEndpointRegistry.java:205) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-6.0.8.jar:6.0.8]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-6.0.8.jar:6.0.8]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-6.0.8.jar:6.0.8]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-6.0.8.jar:6.0.8]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:934) ~[spring-context-6.0.8.jar:6.0.8]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:587) ~[spring-context-6.0.8.jar:6.0.8]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.6.jar:3.0.6]
at md.MyDocsApp.main(MyDocsApp.java:18) ~[main/:na]
Caused by: org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException: AMQ219007: Cannot connect to server(s). Tried with all available servers.
at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:703) ~[artemis-core-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:865) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
... 28 common frames omitted
2023-04-24T09:27:12.777+02:00 INFO 22314 --- [ main] md.MyDocsApp : Started MyDocsApp in 1.947 seconds (process running for 2.267)
Disconnected from the target VM, address: 'localhost:38899', transport: 'socket'
2023-04-24T09:27:17.107+02:00 INFO 22314 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-04-24T09:27:17.117+02:00 ERROR 22314 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:772) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:753) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.6.jar:3.0.6]
at md.MyDocsApp.main(MyDocsApp.java:18) ~[main/:na]
Caused by: org.springframework.jms.UncategorizedJmsException: Uncategorized exception occurred during JMS processing; nested exception is ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ219007: Cannot connect to server(s). Tried with all available servers.]
at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:311) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:184) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:510) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:587) ~[spring-jms-6.0.8.jar:6.0.8]
at md.JmsPublisher.send(JmsPublisher.java:15) ~[main/:na]
at md.MyDocsApp.lambda$send$0(MyDocsApp.java:27) ~[main/:na]
at java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104) ~[na:na]
at java.base/java.util.stream.IntPipeline$Head.forEach(IntPipeline.java:617) ~[na:na]
at md.MyDocsApp.lambda$send$1(MyDocsApp.java:24) ~[main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:769) ~[spring-boot-3.0.6.jar:3.0.6]
... 5 common frames omitted
Caused by: jakarta.jms.JMSException: Failed to create session factory
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:867) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:284) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:279) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:427) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:352) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:327) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:242) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:196) ~[spring-jms-6.0.8.jar:6.0.8]
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:497) ~[spring-jms-6.0.8.jar:6.0.8]
... 12 common frames omitted
Caused by: org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException: AMQ219007: Cannot connect to server(s). Tried with all available servers.
at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:703) ~[artemis-core-client-2.26.0.jar:2.26.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:865) ~[artemis-jakarta-client-2.26.0.jar:2.26.0]
... 20 common frames omitted
2023-04-24T09:27:17.121+02:00 INFO 22314 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-04-24T09:27:17.122+02:00 INFO 22314 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2023-04-24T09:27:17.123+02:00 INFO 22314 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
这个问题的答案其实很简单。我必须取消
gradle.build
文件中这些行的注释:
implementation \
'org.apache.activemq:artemis-jms-server:2.24.0'
(太糟糕了,作者决定不加任何评论地留下这段话......)
这意味着每当您遇到此问题时
ActiveMQNotConnectedException: AMQ219007: Cannot connect to server(s). Tried with all available servers.
检查您是否已启动任何JMS服务器,正如@M-Deinum的评论已经建议的那样。
解决连接问题的另一种方法是确保在本地系统中安装 ActiveMQ Artemis。这是官方下载页面https://activemq.apache.org/components/artemis/download/
下载后,创建经纪人
./artemis create wfmotcslocal --user=username --password=passwordyoulike
并输入
终端上的 ./artemis run
(如果您使用的是 MacOS)。
确保在 application-local.yml 中配置队列详细信息,并且当您启动 Spring Boot 应用程序时,它应该自动创建队列。