Rocketmq-ONS spring 整合
2022年3月18日大约 2 分钟
spring 版本整合
显示文件信息
tree /f
文件目录如下
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─github
│ │ └─houbb
│ │ └─spring
│ │ ConsumerListener.java
│ │ ConsumeWithSpring.java
│ │ ProduceWithSpring.java
│ │
│ └─resources
│ consumer.xml
│ producer.xml
代码
生产者
- producer.xml
xxx
xx
xxx
xxx
- ProduceWithSpring.java
package com.github.houbb.spring;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProduceWithSpring {
public static void main(String[] args) {
/**
* 生产者 Bean 配置在 producer.xml 中,可通过 ApplicationContext 获取或者直接注入到其他类(比如具体的 Controller)中
*/
ApplicationContext context = new ClassPathXmlApplicationContext("producer.xml");
Producer producer = (Producer) context.getBean("producer");
//循环发送消息
for (int i = 0; i
xxx
xxx
xxx
xxx
- ConsumeWithSpring.java
package com.github.houbb.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class ConsumeWithSpring {
public static void main(String[] args) {
/**
* 消费者 Bean 配置在 consumer.xml 中,可通过 ApplicationContext 获取或者直接注入到其他类(比如具体的 Controller)中
*/
ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
System.out.println("Consumer Started");
}
}
参考资料
贡献者
binbin.hou