# 8.1.1 设置 JMS

在使用 JMS 之前，必须将 JMS 客户端添加到项目的构建中。使用 Spring Boot，这个过程简单的不能再简单了，需要做的仅仅是将 starter 依赖添加到构建中。但是，首先必须决定是使用 Apache ActiveMQ，还是使用较新的 Apache ActiveMQ Artemis Broker。

如果使用 ActiveMQ，需要添加以下依赖到项目的 pom.xml 文件中：

```markup
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
```

如果选择 ActiveMQ Artemis，starter 如下所示：

```markup
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
```

Artemis 是 ActiveMQ 的下一代重新实现，实际上这让 ActiveMQ 成为一个遗留选项。因此，对于 Taco Cloud，将选择 Artemis。但是，这种选择最终对如何编写发送和接收消息的代码几乎没有影响。唯一显著的区别在于如何配置 Spring 来创建与 Broker 的连接。

默认情况下，Spring 假设 Artemis Broker 正在监听 localhost 的 61616 端口。对于开发目的，这是可以的，但是一旦准备好将应用程序发送到生产环境中，就需要设置一些属性来告诉 Spring 如何访问代理。表 8.1 列出了最有用的属性。

| 属性                      | 描述                  |
| ----------------------- | ------------------- |
| spring.artemis.host     | broker 主机           |
| spring.artemis.port     | broker 端口           |
| spring.artemis.user     | 用于访问 broker 的用户（可选） |
| spring.artemis.password | 用于访问 broker 的密码（可选） |

例如，考虑应用程序中的以下条目。可能用于非开发设置的 yml 文件：

```yaml
spring:
  artemis:
    host: artemis.tacocloud.com
    port: 61617
    user: tacoweb
    password: 13tm31n
```

这将设置 Spring，以创建到监听 artemis.tacocloud.com（端口 61617）的 Artemis Broker 的 broker 连接。它还设置将与该 broker 交互的应用程序的凭据，凭据是可选的，但建议用于生产部署。

如果要使用 ActiveMQ 而不是 Artemis，则需要使用表 8.2 中列出的 ActiveMQ 特定的属性。

| 属性                         | 描述                     |
| -------------------------- | ---------------------- |
| spring.activemq.broker-url | Broker 的 URL           |
| spring.activemq.user       | 用于访问 Broker 的用户（可选）    |
| spring.activemq.password   | 用于访问 Broker 的密码（可选）    |
| spring.activemq.in-memory  | 是否启动内存 Broker（默认：true） |

请注意，不是为 Broker 的主机名和端口提供单独的属性，而是使用单个属性 spring.activemq.broker-url 指定 ActiveMQ Broker 的地址。URL 应该是 `tcp://` URL，如下面的 YAML 片段所示：

```yaml
spring:
  activemq:
    broker-url: tcp://activemq.tacocloud.com
    user: tacoweb
    password: 13tm31n
```

无论选择 Artemis 还是ActiveMQ，当 Broker 在本地运行时，都不需要为开发环境配置这些属性。

但是，如果使用 ActiveMQ，则需要设置 spring.activemq.in-memory 属性为 false，以防止 Spring 启动内存中的 Broker。内存中的 Broker可能看起来很有用，但它只在发布和消费同一个应用的消息时有用（这一点用处有限）。

在继续之前，将希望安装并启动一个 Artemis（或 ActiveMQ）Broker，而不是使用嵌入式 Broker。与其在这里重复安装说明，我建议你参考 Broker 文档了解详细信息：

* *Artemis* —— <https://activemq.apache.org/artemis/docs/latest/using-server.html>
* *ActiveMQ* —— <http://activemq.apache.org/getting-started.html#GettingStarted-PreInstallationRequirements>

有了构建中的 JMS starter 和等待将消息从一个应用程序传递到另一个应用程序的 Broker，就可以开始发送消息了。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v5/di-8-zhang-fa-song-yi-bu-xiao-xi/8.1-shi-yong-jms-fa-song-xiao-xi/8.1.1-she-zhi-jms.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
