Spring ActiveMQ将JDBC与TCP结合使用

pinkon5k  于 2022-10-23  发布在  Spring
关注(0)|答案(1)|浏览(144)

考虑到我唯一的要求是使用ActiveMQ的示例,我如何让我的ActiveMQ使用JDBC连接,而不创建使用VM传输的嵌入式连接。
这是我的工厂豆子:

@Bean
public ActiveMQConnectionFactory connectionFactory() {
    logger.info("ActiveMQConnectionFactory");
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerUrl);
    activeMQConnectionFactory.setTrustAllPackages(true);
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    redeliveryPolicy.setRedeliveryDelay(15000);
    redeliveryPolicy.setMaximumRedeliveries(-1);
    activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
    return activeMQConnectionFactory;
}

我在url:tcp//0.0.0.0:61616中公开了ACTIVEMQ的图像,但即使配置了如下所示的JDBC适配器,我也不能将消息持久化到SQL服务器中,ACTIVEMQ正在接收此消息,并使用KahaDB作为默认设置。使用JDBC的唯一方法是从tcp更改为vm:localhost,但这会创建一个嵌入的actiemq。

@Bean
public BrokerService broker(DataSource dataSource, ActiveMQConnectionFactory activeMQConnectionFactory) throws Exception {
    logger.info("BrokerService");
    final BrokerService broker = new BrokerService();
    JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter(dataSource, new OpenWireFormat());
    jdbc.setUseLock(true);
    Statements statements = jdbc.getStatements();
    statements.setBinaryDataType(BINARY_DATA_TYPE);
    broker.setUseJmx(true);
    broker.setPersistent(true);
    broker.setPersistenceAdapter(jdbc);
    broker.addConnector(format("vm:(broker:(tcp://localhost:61616,network:static:%s)?persistent=true)", brokerUrl));
    logger.info("BrokerService URL: " + broker.getTransportConnectors().get(0).getConnectUri().toString());
    return broker;
}
ih99xse1

ih99xse11#

建议使用XML文件配置代理。与编写经纪人代码相比,更容易管理。
ActiveMQ JDBC信息:ref:https://activemq.apache.org/jdbc-support
持久性适配器信息:ref:https://activemq.apache.org/persistence
启动引用XML文件的嵌入式代理:ref:https://activemq.apache.org/vm-transport-reference
具体地说:

vm://localhost?brokerConfig=xbean:activemq.xml

相关问题