Search in sources :

Example 1 with BundleReceiver

use of io.openk9.ingestion.api.BundleReceiver 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;
}
Also used : ArrayList(java.util.ArrayList) AMQP(com.rabbitmq.client.AMQP) BindingSpecification(reactor.rabbitmq.BindingSpecification) Binding(io.openk9.ingestion.api.Binding) BundleSender(io.openk9.ingestion.api.BundleSender) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleReceiver(io.openk9.ingestion.api.BundleReceiver)

Aggregations

AMQP (com.rabbitmq.client.AMQP)1 Binding (io.openk9.ingestion.api.Binding)1 BundleReceiver (io.openk9.ingestion.api.BundleReceiver)1 BundleSender (io.openk9.ingestion.api.BundleSender)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Bundle (org.osgi.framework.Bundle)1 BindingSpecification (reactor.rabbitmq.BindingSpecification)1