|
30 | 30 | @Component
|
31 | 31 | public class EventBusRabbitMQ implements EventBus {
|
32 | 32 |
|
33 |
| - private static final String EXCHANGE_TYPE_DIRECT = "direct"; |
34 |
| - private static final String BROKER_NAME = "eshop_event_bus_exchange"; |
35 |
| - private static final String QUEUE_NAME = "Catalog"; |
36 |
| - |
37 |
| - private final Sender sender; |
38 |
| - private final InMemoryEventBusSubscriptionManager subscriptionManager; |
39 |
| - private final Receiver receiver; |
40 |
| - |
41 |
| - @Override |
42 |
| - public Mono<Void> publish(IntegrationEvent event) { |
43 |
| - |
44 |
| - ObjectMapper mapper = new ObjectMapper(); |
45 |
| - String eventName = event.getClass().getSimpleName(); |
46 |
| - BasicProperties basicProperties = new BasicProperties().builder().deliveryMode(2).build(); |
47 |
| - |
48 |
| - try { |
49 |
| - |
50 |
| - byte[] body = mapper.writeValueAsBytes(event); |
51 |
| - OutboundMessage message = new OutboundMessage(BROKER_NAME, eventName, basicProperties, body); |
52 |
| - |
53 |
| - return sender.declare(ExchangeSpecification.exchange(BROKER_NAME).type(EXCHANGE_TYPE_DIRECT)) |
54 |
| - .then(sender.send(Mono.just(message))); |
55 |
| - |
56 |
| - } catch (JsonProcessingException e) { |
57 |
| - return Mono.error(e); |
58 |
| - } |
59 |
| - } |
60 |
| - |
61 |
| - @Override |
62 |
| - public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> Mono<Void> subscribe(Class<T> eventType, |
63 |
| - Class<TH> eventHandler) { |
64 |
| - |
65 |
| - subscriptionManager.addSubscription(eventType, eventHandler); |
66 |
| - log.info("Entered subscribe method"); |
67 |
| - |
68 |
| - Mono<Void> mono = Mono.fromRunnable(() -> { |
69 |
| - receiver.consumeAutoAck(QUEUE_NAME).subscribe(message -> { |
70 |
| - |
71 |
| - String routingKey = message.getEnvelope().getRoutingKey(); |
72 |
| - // Class eventType = subscriptionManager.getEventTypeByName(routingKey); |
73 |
| - byte[] messageBody = message.getBody(); |
74 |
| - try { |
75 |
| - Object event = new ObjectMapper().readValue(messageBody, eventType); |
76 |
| - log.info(event.toString()); |
77 |
| - List<SubscriptionInfo> subscriptions = subscriptionManager.getHandlersForEvent(eventType); |
78 |
| - if (subscriptions != null && !subscriptions.isEmpty()) { |
79 |
| - for (SubscriptionInfo subscription : subscriptions) { |
80 |
| - Class handler = subscription.getHandler(); |
81 |
| - Method methodInfo = null; |
82 |
| - try { |
83 |
| - methodInfo = handler.getDeclaredMethod("handle", IntegrationEvent.class); |
84 |
| - } catch (NoSuchMethodException e) { |
85 |
| - // TODO Auto-generated catch block |
86 |
| - e.printStackTrace(); |
87 |
| - } catch (SecurityException e) { |
88 |
| - // TODO Auto-generated catch block |
89 |
| - e.printStackTrace(); |
90 |
| - } |
91 |
| - |
92 |
| - if(methodInfo != null) |
93 |
| - try { |
94 |
| - methodInfo.invoke(handler.newInstance(), event); |
95 |
| - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { |
96 |
| - // TODO Auto-generated catch block |
97 |
| - e.printStackTrace(); |
98 |
| - } |
99 |
| - } |
100 |
| - |
101 |
| - } |
102 |
| - } catch (IOException e) { |
103 |
| - e.printStackTrace(); |
104 |
| - } |
105 |
| - }); |
106 |
| - }); |
107 |
| - return sender.declare(ExchangeSpecification.exchange(BROKER_NAME)) |
108 |
| - .then(sender.declareQueue(QueueSpecification.queue(QUEUE_NAME))) |
109 |
| - .then(sender.bind(BindingSpecification.binding(BROKER_NAME, eventType.getSimpleName(), QUEUE_NAME))) |
110 |
| - .then(mono); |
111 |
| - |
112 |
| - } |
| 33 | + private static final String EXCHANGE_TYPE_DIRECT = "direct"; |
| 34 | + private static final String BROKER_NAME = "eshop_event_bus_exchange"; |
| 35 | + private static final String QUEUE_NAME = "Catalog"; |
| 36 | + |
| 37 | + private final Sender sender; |
| 38 | + private final InMemoryEventBusSubscriptionManager subscriptionManager; |
| 39 | + private final Receiver receiver; |
| 40 | + |
| 41 | + @Override |
| 42 | + public Mono<Void> publish(IntegrationEvent event) { |
| 43 | + ObjectMapper mapper = new ObjectMapper(); |
| 44 | + String eventName = event.getClass().getSimpleName(); |
| 45 | + System.out.println("Event Name : " + eventName); |
| 46 | + BasicProperties basicProperties = new BasicProperties().builder().deliveryMode(2).build(); |
| 47 | + try { |
| 48 | + byte[] body = mapper.writeValueAsBytes(event); |
| 49 | + OutboundMessage message = new OutboundMessage(BROKER_NAME, eventName, basicProperties, body); |
| 50 | + return sender.declare(ExchangeSpecification.exchange(BROKER_NAME).type(EXCHANGE_TYPE_DIRECT)) |
| 51 | + .then(sender.send(Mono.just(message))); |
| 52 | + } catch (JsonProcessingException e) { |
| 53 | + return Mono.error(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> Mono<Void> subscribe(Class<T> eventType, |
| 59 | + Class<TH> eventHandler) { |
| 60 | + subscriptionManager.addSubscription(eventType, eventHandler); |
| 61 | + log.info("Entered subscribe method"); |
| 62 | + Mono<Void> mono = Mono.fromRunnable(() -> { |
| 63 | + receiver.consumeAutoAck(QUEUE_NAME).subscribe(message -> { |
| 64 | + String routingKey = message.getEnvelope().getRoutingKey(); |
| 65 | + // Class eventType = subscriptionManager.getEventTypeByName(routingKey); |
| 66 | + byte[] messageBody = message.getBody(); |
| 67 | + try { |
| 68 | + Object event = new ObjectMapper().readValue(messageBody, eventType); |
| 69 | + log.info(event.toString()); |
| 70 | + List<SubscriptionInfo> subscriptions = subscriptionManager.getHandlersForEvent(eventType); |
| 71 | + if (subscriptions != null && !subscriptions.isEmpty()) { |
| 72 | + for (SubscriptionInfo subscription : subscriptions) { |
| 73 | + Class handler = subscription.getHandler(); |
| 74 | + Method methodInfo = null; |
| 75 | + try { |
| 76 | + methodInfo = handler.getDeclaredMethod("handle", IntegrationEvent.class); |
| 77 | + } catch (NoSuchMethodException e) { |
| 78 | + // TODO Auto-generated catch block |
| 79 | + e.printStackTrace(); |
| 80 | + } catch (SecurityException e) { |
| 81 | + // TODO Auto-generated catch block |
| 82 | + e.printStackTrace(); |
| 83 | + } |
| 84 | + |
| 85 | + if (methodInfo != null) |
| 86 | + try { |
| 87 | + methodInfo.invoke(handler.newInstance(), event); |
| 88 | + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException |
| 89 | + | InstantiationException e) { |
| 90 | + // TODO Auto-generated catch block |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + } catch (IOException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + return sender.declare(ExchangeSpecification.exchange(BROKER_NAME)) |
| 102 | + .then(sender.declareQueue(QueueSpecification.queue(QUEUE_NAME))) |
| 103 | + .then(sender.bind(BindingSpecification.binding(BROKER_NAME, eventType.getSimpleName(), QUEUE_NAME))) |
| 104 | + .then(mono); |
| 105 | + |
| 106 | + } |
113 | 107 |
|
114 | 108 | }
|
0 commit comments