Skip to content

Commit 8a736bf

Browse files
author
Frank Oh
committed
studying rabbitmq... does not work properly
1 parent 333eb3a commit 8a736bf

14 files changed

Lines changed: 308 additions & 133 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//package com.advenoh.config;
2+
//
3+
//import lombok.extern.slf4j.Slf4j;
4+
//import org.springframework.amqp.core.Queue;
5+
//import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
6+
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
7+
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
8+
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
9+
//import org.springframework.beans.factory.annotation.Value;
10+
//import org.springframework.context.annotation.Bean;
11+
//import org.springframework.context.annotation.Configuration;
12+
//import org.springframework.context.annotation.PropertySource;
13+
//import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
14+
//
15+
//@Slf4j
16+
//@Configuration
17+
//@PropertySource("classpath:application.properties")
18+
//public class RabbitMQConfig {
19+
//
20+
// @Value("${rabbitmq.queue.name}")
21+
// public String queueName;
22+
//
23+
// @Value("${rabbitmq.exchange.name}")
24+
// public String exchangeName;
25+
//
26+
// @Value("${rabbitmq.host}")
27+
// public String host;
28+
//
29+
// @Value("${rabbitmq.port}")
30+
// public String port;
31+
//
32+
// @Bean
33+
// public ConnectionFactory connectionFactory() {
34+
// CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
35+
// return connectionFactory;
36+
// }
37+
//
38+
// @Bean
39+
// public RabbitTemplate rabbitTemplate() {
40+
// return new RabbitTemplate(connectionFactory());
41+
// }
42+
//
43+
// @Bean
44+
// public Queue myQueue() {
45+
// return new Queue(queueName);
46+
// }
47+
//
48+
// @Bean(name = "rabbitListenerContainerFactory")
49+
// public SimpleRabbitListenerContainerFactory rabbitListenerContainerlistenerFactory() {
50+
// SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
51+
// factory.setConnectionFactory(connectionFactory());
52+
// return factory;
53+
// }
54+
//
55+
// @Bean
56+
// public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
57+
// return new PropertySourcesPlaceholderConfigurer();
58+
// }
59+
//}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.advenoh.queue.nonspring;
2+
3+
import org.springframework.amqp.core.BindingBuilder;
4+
import org.springframework.amqp.core.DirectExchange;
5+
import org.springframework.amqp.core.Queue;
6+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
7+
import org.springframework.amqp.rabbit.core.RabbitAdmin;
8+
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
9+
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
10+
11+
public class Consumer {
12+
public static void main(String[] args) {
13+
//RabbitMQ 연결
14+
CachingConnectionFactory cf = new CachingConnectionFactory("127.0.0.1", 5672);
15+
cf.setUsername("guest");
16+
cf.setPassword("guest");
17+
18+
//큐 생성
19+
RabbitAdmin admin = new RabbitAdmin(cf);
20+
Queue queue = new Queue("myQueue");
21+
admin.declareQueue(queue);
22+
23+
//Exchange에 바인딩
24+
DirectExchange exchange = new DirectExchange("amq.direct");
25+
admin.declareBinding(BindingBuilder.bind(queue).to(exchange)
26+
.with("foo.bar"));
27+
28+
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
29+
Object listener = new Object() {
30+
//메시지 처리
31+
public void handleMessage(Object foo) {
32+
System.out.println(foo);
33+
}
34+
};
35+
36+
//메시지 리스닝
37+
MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
38+
container.setMessageListener(adapter);
39+
container.setQueueNames("myQueue");
40+
container.start();
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.advenoh.queue.nonspring;
2+
3+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
4+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
5+
6+
public class Producer {
7+
public static void main(final String[] args) {
8+
//RabbitMQ 연결
9+
CachingConnectionFactory cf
10+
= new CachingConnectionFactory("127.0.0.1", 5672);
11+
cf.setUsername("guest");
12+
cf.setPassword("guest");
13+
14+
//메시지 보내기
15+
RabbitTemplate template = new RabbitTemplate(cf);
16+
template.setExchange("amq.direct");
17+
template.setQueue("myQueue");
18+
template.convertAndSend("foo.bar", "Hello, world!");
19+
cf.destroy();
20+
}
21+
}
22+
23+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.advenoh.queue.simple;
2+
3+
import org.springframework.amqp.rabbit.annotation.Exchange;
4+
import org.springframework.amqp.rabbit.annotation.Queue;
5+
import org.springframework.amqp.rabbit.annotation.QueueBinding;
6+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.concurrent.CountDownLatch;
10+
11+
//@RabbitListener(queues = "test.queue")
12+
@Component
13+
public class MyService {
14+
private CountDownLatch countDownLatch = new CountDownLatch(1);
15+
16+
@RabbitListener(bindings = @QueueBinding(
17+
value = @Queue(value = "test.queue", durable = "false"),
18+
exchange = @Exchange(value = "test.exchange"))
19+
)
20+
public void receiveMsg(String message) {
21+
System.out.println("Message Received: " + message);
22+
countDownLatch.countDown();
23+
}
24+
25+
public CountDownLatch getCountDownLatch() {
26+
return countDownLatch;
27+
}
28+
}
29+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//package com.advenoh.queue.simple;
2+
//
3+
//import com.advenoh.config.RabbitMQConfig;
4+
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
5+
//import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
//
7+
//import java.util.concurrent.TimeUnit;
8+
//
9+
//public class Sender {
10+
// final static String queueName = "test.queue";
11+
//
12+
// public static void main(String[] args) throws InterruptedException {
13+
// AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
14+
// ctx.register(RabbitMQConfig.class);
15+
// ctx.refresh();
16+
//
17+
// System.out.println("---Message is being sent---");
18+
// RabbitTemplate rabbitTemplate = (RabbitTemplate) ctx.getBean("rabbitTemplate");
19+
// MyService receiver = (MyService) ctx.getBean("receiver");
20+
//
21+
// rabbitTemplate.convertAndSend(queueName, "Hello World!");
22+
// receiver.getCountDownLatch().await(1, TimeUnit.SECONDS);
23+
//
24+
// ctx.close();
25+
// }
26+
//}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.advenoh.queue.spring;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.stereotype.Component;
5+
6+
@Slf4j
7+
@Component
8+
public class Consumer {
9+
//메시지를 처리한다.
10+
public void handleMessage(Object message) {
11+
log.info("message: {}", message);
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.advenoh.queue.spring;
2+
3+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class Producer {
9+
10+
@Autowired
11+
private RabbitTemplate rabbitTemplate;
12+
13+
public void sendMessage(Object message) {
14+
//메시지를 보낸다.
15+
rabbitTemplate.convertAndSend(message);
16+
}
17+
}

spring-rabbitmq/src/main/java/com/concretepage/rabbitmq/Main.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

spring-rabbitmq/src/main/java/com/concretepage/rabbitmq/MessageReceiver.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

spring-rabbitmq/src/main/java/com/concretepage/rabbitmq/RabbitMQConfig.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)