use of io.openk9.osgi.util.AutoCloseables in project openk9 by smclab.
the class BindingServiceTrackerCustomizer method addingService.
@Override
public Binding addingService(ServiceReference<Binding> reference) {
Binding service = _bundleContext.getService(reference);
Bundle bundle = reference.getBundle();
Binding.Exchange exchangeDTO = service.getExchange();
String exchange = exchangeDTO.getName();
String routingKey = service.getRoutingKey();
String queue = service.getQueue();
Binding.Exchange.Type exchangeType = exchangeDTO.getType();
Mono<AMQP.Exchange.DeclareOk> mono1 = _sender.declareExchange(ExchangeSpecification.exchange(exchange).type(exchangeType.name()).durable(exchange.startsWith("amq") || service.exchangeDurable()));
List<AutoCloseable> autoCloseables = new ArrayList<>();
if (queue != null) {
Mono<AMQP.Queue.DeclareOk> mono2 = _sender.declareQueue(QueueSpecification.queue(queue).durable(service.queueDurable()).arguments(service.getArguments()));
BindingSpecification binding = BindingSpecification.binding(exchange, routingKey, queue);
autoCloseables.add(() -> _sender.unbind(binding).subscribe());
Mono<AMQP.Queue.BindOk> mono3 = _sender.bind(binding);
if (Schedulers.isInNonBlockingThread()) {
Mono.zip(mono1, mono2, mono3).subscribe();
} else {
Mono.zip(mono1, mono2, mono3).block();
}
} else {
if (Schedulers.isInNonBlockingThread()) {
mono1.subscribe();
} else {
mono1.block();
}
}
_log.info(String.format("Bundle: %s, Service: %s, exchange: %s, exchange type: %s, " + "routingKey: %s, queue: %s", bundle.getSymbolicName(), service.getClass().getName(), exchange, exchangeType.name(), routingKey, queue));
Dictionary<String, Object> senderProps = new Hashtable<>();
senderProps.put("exchange", exchange);
if (routingKey != null) {
senderProps.put("routingKey", routingKey);
}
if (queue != null) {
senderProps.put("queue", queue);
}
senderProps.put("exchangeType", exchangeType.name());
ServiceRegistration<BundleSender> bundleSenderRegistration = bundle.getBundleContext().registerService(BundleSender.class, new BundleSenderImpl(_sender, exchange, routingKey), senderProps);
autoCloseables.add(bundleSenderRegistration::unregister);
if (queue != null) {
ServiceRegistration<BundleReceiver> bundleReceiverRegistration = bundle.getBundleContext().registerService(BundleReceiver.class, new BundleReceiverImpl(_receiverReactor, queue), new Hashtable<>(Collections.singletonMap("queue", queue)));
autoCloseables.add(bundleReceiverRegistration::unregister);
}
_registrationMap.put(bundle, AutoCloseables.mergeAutoCloseableToSafe(autoCloseables));
return null;
}
Aggregations