You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在JUnit 5测试中嵌入运行支持OpenWire协议的Jakarta ActiveMQ Artemis?

可行解决方案

方案1:手动构建Jakarta兼容的OpenWire协议模块

  • 克隆Apache ActiveMQ Artemis官方仓库,切换到与你使用的artemis-jakarta-server版本一致的分支
  • 找到artemis-openwire-protocol模块,将所有Java EE依赖替换为Jakarta EE对应依赖(比如javax.jms:javax.jms-api替换为jakarta.jms:jakarta.jms-api,javax.annotation:javax.annotation-api替换为jakarta.annotation:jakarta.annotation-api)
  • 调整模块的pom.xml,添加Jakarta迁移相关配置,确保编译输出兼容Jakarta规范
  • 本地构建该模块并安装到本地Maven仓库,之后在测试项目中引入此依赖

方案2:自定义JUnit 5扩展适配Jakarta

  • 放弃依赖官方artemis-junit-5,自行实现简单的JUnit 5扩展来启动嵌入式Artemis服务器
  • 核心实现步骤:
    1. 在扩展的beforeAll或beforeEach阶段,创建org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ实例
    2. 通过Configuration对象配置OpenWire协议:添加acceptor,设置uri为tcp://localhost:61616?protocols=OPENWIRE
    3. 确保服务器配置使用Jakarta版本的JMS依赖,避免API冲突
    4. 在afterAll或afterEach阶段停止并清理嵌入式服务器
  • 示例代码片段:
public class EmbeddedArtemisExtension implements BeforeAllCallback, AfterAllCallback {
    private EmbeddedActiveMQ embeddedServer;

    @Override
    public void beforeAll(ExtensionContext context) throws Exception {
        Configuration config = new ConfigurationImpl();
        config.addAcceptorConfiguration("openwire", "tcp://localhost:61616?protocols=OPENWIRE");
        config.setPersistenceEnabled(false);
        config.setSecurityEnabled(false);

        embeddedServer = new EmbeddedActiveMQ();
        embeddedServer.setConfiguration(config);
        embeddedServer.start();
        // 按需创建测试用队列/主题
        embeddedServer.getActiveMQServer().createQueue(new QueueConfiguration("testQueue"));
    }

    @Override
    public void afterAll(ExtensionContext context) throws Exception {
        if (embeddedServer != null) {
            embeddedServer.stop();
        }
    }
}
  • 在测试类上添加@ExtendWith(EmbeddedArtemisExtension.class)即可启用

方案3:临时使用独立OpenWire兼容代理测试

  • 下载支持Jakarta的Apache ActiveMQ Classic过渡版本,在测试前启动该独立代理
  • 测试代码中配置JMS客户端连接至该代理,测试完成后停止代理
  • 可通过exec-maven-plugin等Maven插件,在测试阶段自动完成代理的启动与停止,无需手动操作

内容的提问来源于stack exchange,提问作者Mats

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 13:32:08