網(wǎng)站優(yōu)化哪家好網(wǎng)絡(luò)營(yíng)銷做的好的企業(yè)
原始狀態(tài)的 activemq-client sdk 集成非常方便,也更適合定制。就是有些同學(xué),可能對(duì)原始接口會(huì)比較陌生,會(huì)希望有個(gè)具體的示例。
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>${activemq.version}</version>
</dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>${activemq.version}</version>
</dependency>
希望更加簡(jiǎn)化使用的同學(xué),可以使用:
activemq-solon-cloud-plugin (使用更簡(jiǎn)單,定制性弱些)
1、添加集成配置
先使用 Solon 初始器 先生成一個(gè) Solon Web 模板項(xiàng)目,然后添加上面的 activemq-client 依賴。再做個(gè)配置約定(也可按需定義):
- “solon.activemq”,作為配置前綴
- “properties”,作為公共配置
- “producer”,作為生態(tài)者專屬配置(估計(jì)用不到)
- “consumer”,作為消費(fèi)者專屬配置(估計(jì)用不到)
具體的配置屬性,參考自:ActiveMQConnectionFactory
solon.app:name: "demo-app"group: "demo"# 配置可以自由定義,與 @Bean 代碼對(duì)應(yīng)起來即可(以下為參考)
solon.activemq:properties: #公共配置(配置項(xiàng),參考:ActiveMQConnectionFactory)brokerURL: "failover:tcp://localhost:61616"redeliveryPolicy:initialRedeliveryDelay: 5000backOffMultiplier: 2useExponentialBackOff: truemaximumRedeliveries: -1maximumRedeliveryDelay: 3600_000
添加 java 配置器
@Configuration
public class ActivemqConfig {@Bean(destroyMethod = "stop")public Connection client(@Inject("${solon.activemq.properties}") Props common) throws Exception {String brokerURL = (String) common.remove("brokerURL");String userName = (String) common.remove("userName");String password = (String) common.remove("password");ActiveMQConnectionFactory factory;if (Utils.isEmpty(userName)) {factory = new ActiveMQConnectionFactory(brokerURL);} else {factory = new ActiveMQConnectionFactory(brokerURL, userName, password);}//綁定額外的配置并創(chuàng)建連接Connection connection = common.bindTo(factory).createConnection();connection.start();return connection;}@Beanpublic IProducer producer(Connection connection) throws Exception {return new IProducer(connection);}@Beanpublic void consumer(Connection connection,MessageListener messageListener) throws Exception {Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);Destination destination = session.createTopic("topic.test");MessageConsumer consumer = session.createConsumer(destination);consumer.setMessageListener(messageListener);}
}
activemq 的消息發(fā)送的代碼比較復(fù)雜,所以我們可以做個(gè)包裝處理(用于上面的配置構(gòu)建),臨時(shí)命名為 IProducer:
public class IProducer {private Connection connection;public IProducer(Connection connection) {this.connection = connection;}public void send(String topic, MessageBuilder messageBuilder) throws JMSException {Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);Destination destination = session.createTopic(topic);MessageProducer producer = session.createProducer(destination);producer.send(destination, messageBuilder.build(session));}@FunctionalInterfacepublic static interface MessageBuilder {Message build(Session session) throws JMSException;}
}
3、代碼應(yīng)用
發(fā)送(或生產(chǎn)),這里代控制器由用戶請(qǐng)求再發(fā)送消息(僅供參考):
@Controller
public class DemoController {@Injectprivate IProducer producer;@Mapping("/send")public void send(String msg) throws Exception {//發(fā)送producer.send("topic.test", s -> s.createTextMessage("test"));}
}
監(jiān)聽(或消費(fèi)),這里采用訂閱回調(diào)的方式:(僅供參考)
@Component
public class DemoMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {System.out.println(message);RunUtil.runAndTry(message::acknowledge);}
}