Change the conf to make this can run as spring boot
This commit is contained in:
parent
9afc07eb28
commit
a13f0afaa5
|
@ -0,0 +1,87 @@
|
|||
package com.ossez.springamqp.simple;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AMQPHelloWorldMessageApp {
|
||||
|
||||
private static final boolean NON_DURABLE = true;
|
||||
private static final String MY_QUEUE_NAME = "com.ossez.real.estate";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
|
||||
// AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
|
||||
//
|
||||
// amqpTemplate.convertAndSend("Hello World");
|
||||
// System.out.println("Sent: Hello World");
|
||||
|
||||
SpringApplication.run(AMQPHelloWorldMessageApp.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("158.69.60.50");
|
||||
connectionFactory.setUsername("admin");
|
||||
connectionFactory.setPassword("Lucas#1120");
|
||||
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
|
||||
RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
||||
//The routing key is set to the name of the queue by the broker for the default exchange.
|
||||
|
||||
|
||||
template.setRoutingKey(MY_QUEUE_NAME);
|
||||
|
||||
//Where we will synchronously receive messages from
|
||||
template.setDefaultReceiveQueue(MY_QUEUE_NAME);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
Queue myQueue() {
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
// // set the queue with a dead letter feature
|
||||
// args.put("x-queue-type", "classic");
|
||||
|
||||
return new Queue(MY_QUEUE_NAME, NON_DURABLE, false, false, args);
|
||||
// return new Queue(MY_QUEUE_NAME, NON_DURABLE);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner runner(RabbitTemplate template) {
|
||||
return args -> {
|
||||
for (int i =0; i<1000; i++) {
|
||||
template.convertAndSend("Hello, world- YuCheng Hu!"+ i);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@RabbitListener(queues = MY_QUEUE_NAME)
|
||||
public void listen(String in) {
|
||||
System.out.println("Message read from myQueue : " + in);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue