这个东西没接触到的时候挺懵的,用过一次之后,哇哦
public class Producter {
//ActiveMq 的默认用户名
private static final String USERNAME = "admin";
//ActiveMq 的默认登录密码
private static final String PASSWORD = "admin";
//ActiveMQ 的链接地址
private static final String BROKEN_URL = "failover://tcp://10.10.129.130:61616";
AtomicInteger count = new AtomicInteger(0);
//链接工厂
ConnectionFactory connectionFactory;
//链接对象
Connection connection;
//事务管理
Session session;
ThreadLocal<MessageProducer> threadLocal = new ThreadLocal<>();
public void init() {
try {
//创建一个链接工厂
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
//从工厂中创建一个链接
connection = connectionFactory.createConnection();
//开启链接
connection.start();
//创建一个事务(这里通过参数可以设置事务的级别)
session = connection.createSession(true, Session.SESSION_TRANSACTED);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void sendMessage(String disname, List<LgtTimePlanEntity> list) {
try {
//创建一个消息队列
Queue queue = session.createQueue(disname);
//消息生产者
MessageProducer messageProducer = null;
if (threadLocal.get() != null) {
messageProducer = threadLocal.get();
} else {
messageProducer = session.createProducer(queue);
threadLocal.set(messageProducer);
}
Thread.sleep(1000);
int num = count.getAndIncrement();
for (LgtTimePlanEntity entity : list) {
String phone = entity.getPhone();
String orderId = entity.getOrderId();
String str = phone + "&" + orderId;
//创建一条消息
TextMessage msg = session.createTextMessage(str);
System.out.println(msg.getText());
//发送消息
messageProducer.send(msg);
//提交事务
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
首先创建一个生产者,这东西其实就是一个工具类
@Service(value = "lgtWayBillInfoService")
public class LgtWayBillInfoServiceImpl implements LgtWayBillInfoService {
@Autowired
private LgtTimePlanEntityMapper lgtTimePlanEntityMapper;
@Override
@Scheduled(cron = "0/5 * * * * ?")
public void queryLgtWayBillInfo() {
Date now = new Date();
Long time = now.getTime();
String ts = time.toString();
List<LgtTimePlanEntity> list = lgtTimePlanEntityMapper.selectPhone(ts);
Producter producter = new Producter();
producter.init();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
producter.sendMessage("ActiveMQ",list);
int a = lgtTimePlanEntityMapper.deletePhone(ts);
}
}
然后在方法执行中调用sendMessage方法,这不就是一个工具类么,这时候已经把数据放进队列中了,接下来就是从队列中发送出去数据
public class Comsumer {
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
private static final String BROKEN_URL = "failover://tcp://10.10.129.130:61616";
ConnectionFactory connectionFactory;
Connection connection;
Session session;
ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<>();
AtomicInteger count = new AtomicInteger();
public void init(){
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void getMessage(String disname){
try {
Queue queue = session.createQueue(disname);
MessageConsumer consumer = null;
if(threadLocal.get()!=null){
consumer = threadLocal.get();
}else{
consumer = session.createConsumer(queue);
threadLocal.set(consumer);
}
while(true){
Thread.sleep(1000);
TextMessage msg = (TextMessage) consumer.receive();
if(msg!=null) {
msg.acknowledge();
String str = msg.getText();
// String[] numArray = str.split("&");
// String phone = numArray[0];
// String orderId = numArray[1];
System.out.println(Thread.currentThread().getName()+": Consumer:我是消费者,我正在消费Msg"+str+"--->"+count.getAndIncrement());
}else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
首先创建一个消费者,说白了这玩意也是一个工具类
@SpringBootApplication
public class LogisticsplatformIBMPApplication {
public static void main(String[] args) {
SpringApplication.run(LogisticsplatformIBMPApplication.class, args);
Comsumer comsumer = new Comsumer();
comsumer.init();
LogisticsplatformIBMPApplication testConsumer = new LogisticsplatformIBMPApplication();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
}
private class ConsumerMq implements Runnable {
Comsumer comsumer;
public ConsumerMq(Comsumer comsumer) {
this.comsumer = comsumer;
}
@Override
public void run() {
while (true) {
try {
comsumer.getMessage("ActiveMQ");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
然后,重头戏来了,我把工程中的启动类改了 ,这样启动工程的时候消费者就可以从生产者产生的数据里直接取,而且要注意,消息队列是需要账号密码和ip端口才能连接的,所以在一个完整的项目中,MQ可能只有一个,但是生产者和消费者不一定有多少个