用groovy 实现MQ信息的发送和消费。如果创建一个MQ消费对象。
时间: 2023-05-12 22:05:29 浏览: 104
可以使用Groovy的ActiveMQ库来实现MQ信息的发送和消费。以下是一个简单的示例代码:
// 导入ActiveMQ库
@Grab(group='org.apache.activemq', module='activemq-all', version='5.15.9')
// 创建MQ连接
def factory = new org.apache.activemq.ActiveMQConnectionFactory('tcp://localhost:61616')
def connection = factory.createConnection()
connection.start()
// 创建MQ会话
def session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE)
// 创建MQ目的地
def destination = session.createQueue('myQueue')
// 创建MQ消费者
def consumer = session.createConsumer(destination)
// 注册消息监听器
consumer.setMessageListener { message ->
println "Received message: ${message.text}"
}
// 创建MQ生产者
def producer = session.createProducer(destination)
// 发送消息
def text = 'Hello, World!'
def textMessage = session.createTextMessage(text)
producer.send(textMessage)
// 关闭MQ连接
producer.close()
consumer.close()
session.close()
connection.close()
至于创建一个MQ消费对象,可以参考上面的代码中的consumer对象的创建方式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)