windows下, artemis学习

1. download artemis from apache

ActiveMQhttps://activemq.apache.org/components/artemis/download/2. 解压缩到 C:/software/apache-artemis-2.30.0/

2. 进入到cmd, 执行

C:\software\apache-artemis-2.30.0\bin>artemis create  C:/software/apache-artemis-2.30.0/xbroker --windows

xbroker被创建

3. 进入broker,启动

C:\software\apache-artemis-2.30.0\xbroker\bin>artemis run

 4. 进入控制台

http://localhost:8161/console

启动成功, 进入创建,待续

5. 建立一个springboot project, 使用web starter和 AcitveMQ Artemis

 pom.xm以下依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-artemis</artifactId>

</dependency>

配置application.propertis

spring.application.name=artemisMq

server.servlet.context-path=/artemisMq

server.port=9090

blue.mq.in=blueQueue

spring.artemis.broker-url=tcp://localhost:61616

spring.artemis.user=blue

#spring.artemis.mode=native

#spring.artemis.host=localhost

#spring.artemis.port=61616

#spring.artemis.password=blue

#spring.artemis.pool.enabled=true

#spring.artemis.pool.max-connections=50

#spring.artemis.embedded.enabled=false

创建msg sender

@Log4j2

@Service

public class BlueQueueSender {

@Autowired

JmsTemplate jmsTemplate;

@Value("${blue.mq.in}")

String queueName;

public void sendMsg(String msg) throws Exception {

jmsTemplate.convertAndSend(queueName, msg);

log.info("Msg is sent to queue[" + queueName + "] successfully!");

}

}

创建接收者

@Log4j2

@Component

@EnableJms

public class BlueQueueListener {

@Autowired

BlueQueueSender msgService;

@JmsListener(destination = "${blue.mq.in}")

public void onMsg(Message msg) {

try {

Thread.sleep(1000);

String msgBody = msg.getBody(String.class);

System.out.println("******************************receive msg :"+msgBody);

if (StringUtils.isBlank(msgBody)) {

throw new Exception("Message body is empty!");

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

创建一个Rest Method

@RestController

@SpringBootApplication

public class ArtemisMqApplication {

@Autowired

BlueQueueSender msgSvc;

public static void main(String[] args) {

SpringApplication.run(ArtemisMqApplication.class, args);

}

@PostMapping("/sendMsg")

public String sendMsg(@RequestBody String msg) throws Exception {

msgSvc.sendMsg(msg);

return "success";

}

}

使用postman发送消息,可以看到Listener打印出接收到消息

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值