public void detach(ChannelSource source, PhysicalConnectionTarget target) throws Fabric3Exception { if (target.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = source.getUri(); Channel channel = getChannel(uri, source.getChannelSide()); URI targetUri = target.getUri(); channel.unsubscribe(targetUri, target.getTopic()); }
private void addTypeTransformer(PhysicalChannelConnection connection, EventStream stream, ClassLoader loader) { if (transformerHandlerFactory == null) { return; // bootstrap } List<DataType> sourceTypes = connection.getSource().getDataTypes(); List<DataType> targetTypes = connection.getTarget().getDataTypes(); if (sourceTypes.isEmpty() || targetTypes.isEmpty()) { return; } if (sourceTypes.size() > 1 || targetTypes.size() > 1) { // for now, only support one data type throw new Fabric3Exception("Multi-type events are not supported"); } DataType sourceType = sourceTypes.get(0); DataType targetType = targetTypes.get(0); if (sourceType.equals(targetType)) { return; } List<Class<?>> eventTypes = Collections.singletonList(stream.getEventType()); EventStreamHandler handler = transformerHandlerFactory.createHandler(sourceType, targetType, eventTypes, loader); stream.addHandler(handler); }
@SuppressWarnings({"unchecked"}) public void disconnect(PhysicalChannelConnection physicalConnection) { PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); Key key = new Key(source.getSourceId(), target.getTargetId()); Holder holder = cachedConnections.get(key); if (holder == null) { return; } if (holder.count.decrementAndGet() == 0) { cachedConnections.remove(key); } else { return; } SourceConnectionAttacher sourceAttacher = sourceAttachers.get(source.getClass()); if (sourceAttacher == null) { throw new Fabric3Exception("Attacher not found for type: " + source.getClass().getName()); } TargetConnectionAttacher targetAttacher = targetAttachers.get(target.getClass()); if (targetAttacher == null) { throw new Fabric3Exception("Attacher not found for type: " + target.getClass().getName()); } sourceAttacher.detach(source, target); targetAttacher.detach(source, target); }
public void attach(ChannelSource source, PhysicalConnectionTarget target, ChannelConnection connection) throws Fabric3Exception { connection.setCloseable(() -> { // no-op }); if (target.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = source.getUri(); Channel channel = getChannel(uri, source.getChannelSide()); URI targetUri = target.getUri(); channel.subscribe(targetUri, connection); }
PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); if (source.isDirectConnection() || target.isDirectConnection()) { if (physicalConnection.isBound()) { Class<?> type = source.isDirectConnection() ? source.getServiceInterface() : target.getServiceInterface(); DirectConnectionFactory factory = connectionFactories.get(type); if (factory == null) { } else { ClassLoader loader = physicalConnection.getTarget().getClassLoader(); Class<?> eventType = physicalConnection.getEventType(); EventStream stream = new EventStreamImpl(eventType);
public void attach(NATSConnectionSource source, PhysicalConnectionTarget target, ChannelConnection connection) { String topic = source.getTopic() != null ? source.getTopic() : source.getDefaultTopic(); connection.setCloseable(() -> connectionManager.release(source.getChannelUri(), topic)); if (target.isDirectConnection()) { Class<?> type = target.getServiceInterface(); connectionManager.createDirectConsumer(type, source); // create consumer, which will be returned by the direct connection factory } else { String deserializerName = source.getData().getDeserializer(); Function deserializer = deserializerName != null ? InstanceResolver.getInstance(deserializerName, info, cm) : null; connection.getEventStream().addHandler(0, new EventStreamHandler() { private EventStreamHandler next; @SuppressWarnings("unchecked") public void handle(Object message, boolean batch) { Object body = deserializer != null ? deserializer.apply(message) : message; next.handle(body, batch); } public void setNext(EventStreamHandler next) { this.next = next; } public EventStreamHandler getNext() { return next; } }); connectionManager.subscribe(source, connection); } }
public List<PhysicalChannelConnection> generateConsumer(LogicalConsumer consumer, Map<LogicalChannel, DeliveryType> channels) { LogicalComponent<?> component = consumer.getParent(); ComponentGenerator<?> generator = getGenerator(component); PhysicalConnectionTarget target = generator.generateConnectionTarget(consumer); URI classLoaderId = component.getDefinition().getContributionUri(); ClassLoader classLoader = classLoaderRegistry.getClassLoader(classLoaderId); target.setClassLoader(classLoader); if (isDirect(consumer, channels)) { target.setDirectConnection(true); return generateDirectConnections(consumer, channels, target, classLoader); } else { return generateConnections(consumer, channels, target, classLoader); } }
public Object subscribe(Class<?> type, String name, String id, String topic, java.util.function.Consumer<?> consumer) { LogicalChannel logicalChannel = getChannel(name); LogicalConsumer logicalConsumer = createConsumer(type, logicalChannel.getUri()); PhysicalChannel physicalChannel = channelGenerator.generate(logicalChannel, SYNTHETIC_CONTRIBUTION, CONSUMER); channelBuilderRegistry.build(physicalChannel); Map<LogicalChannel, DeliveryType> channels = Collections.singletonMap(logicalChannel, DeliveryType.DEFAULT); List<PhysicalChannelConnection> connections = connectionGenerator.generateConsumer(logicalConsumer, channels); // Two connections will be created: one from the binding to the channel, the other from the channel to the component // the closeable will be set on the connection from the binding to the channel; it must be passed to the component Closeable closeable = null; for (PhysicalChannelConnection connection : connections) { connection.setTopic(topic); connection.getSource().setTopic(topic); connection.getTarget().setTopic(topic); connection.getTarget().setConsumer(consumer); ChannelConnection channelConnection = channelConnector.connect(connection); if (channelConnection.getCloseable() != null) { closeable = channelConnection.getCloseable(); } } return closeable; }
private PhysicalChannelConnection generateLocalConnection(LogicalProducer producer, Class<?> type, LogicalChannel channel, PhysicalConnectionSource source, ClassLoader classLoader) { URI uri = channel.getUri(); PhysicalConnectionTarget target = new ChannelTarget(uri, ChannelSide.PRODUCER); target.setClassLoader(classLoader); boolean bound = channel.isBound(); return new PhysicalChannelConnection(uri, producer.getUri(), source, target, type, bound); }
PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); if (source.isDirectConnection() || target.isDirectConnection()) { if (physicalConnection.isBound()) { Class<?> type = source.isDirectConnection() ? source.getServiceInterface() : target.getServiceInterface(); DirectConnectionFactory factory = connectionFactories.get(type); if (factory == null) { } else { ClassLoader loader = physicalConnection.getTarget().getClassLoader(); Class<?> eventType = physicalConnection.getEventType(); EventStream stream = new EventStreamImpl(eventType);
public void attach(ChannelSource source, PhysicalConnectionTarget target, ChannelConnection connection) throws Fabric3Exception { connection.setCloseable(() -> { // no-op }); if (target.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = source.getUri(); Channel channel = getChannel(uri, source.getChannelSide()); URI targetUri = target.getUri(); channel.subscribe(targetUri, connection); }
public List<PhysicalChannelConnection> generateConsumer(LogicalConsumer consumer, Map<LogicalChannel, DeliveryType> channels) { LogicalComponent<?> component = consumer.getParent(); ComponentGenerator<?> generator = getGenerator(component); PhysicalConnectionTarget target = generator.generateConnectionTarget(consumer); URI classLoaderId = component.getDefinition().getContributionUri(); ClassLoader classLoader = classLoaderRegistry.getClassLoader(classLoaderId); target.setClassLoader(classLoader); if (isDirect(consumer, channels)) { target.setDirectConnection(true); return generateDirectConnections(consumer, channels, target, classLoader); } else { return generateConnections(consumer, channels, target, classLoader); } }
public Object subscribe(Class<?> type, String name, String id, String topic, java.util.function.Consumer<?> consumer) { LogicalChannel logicalChannel = getChannel(name); LogicalConsumer logicalConsumer = createConsumer(type, logicalChannel.getUri()); PhysicalChannel physicalChannel = channelGenerator.generate(logicalChannel, SYNTHETIC_CONTRIBUTION, CONSUMER); channelBuilderRegistry.build(physicalChannel); Map<LogicalChannel, DeliveryType> channels = Collections.singletonMap(logicalChannel, DeliveryType.DEFAULT); List<PhysicalChannelConnection> connections = connectionGenerator.generateConsumer(logicalConsumer, channels); // Two connections will be created: one from the binding to the channel, the other from the channel to the component // the closeable will be set on the connection from the binding to the channel; it must be passed to the component Closeable closeable = null; for (PhysicalChannelConnection connection : connections) { connection.setTopic(topic); connection.getSource().setTopic(topic); connection.getTarget().setTopic(topic); connection.getTarget().setConsumer(consumer); ChannelConnection channelConnection = channelConnector.connect(connection); if (channelConnection.getCloseable() != null) { closeable = channelConnection.getCloseable(); } } return closeable; }
private PhysicalChannelConnection generateLocalConnection(LogicalProducer producer, Class<?> type, LogicalChannel channel, PhysicalConnectionSource source, ClassLoader classLoader) { URI uri = channel.getUri(); PhysicalConnectionTarget target = new ChannelTarget(uri, ChannelSide.PRODUCER); target.setClassLoader(classLoader); boolean bound = channel.isBound(); return new PhysicalChannelConnection(uri, producer.getUri(), source, target, type, bound); }
public void detach(ChannelSource source, PhysicalConnectionTarget target) throws Fabric3Exception { if (target.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = source.getUri(); Channel channel = getChannel(uri, source.getChannelSide()); URI targetUri = target.getUri(); channel.unsubscribe(targetUri, target.getTopic()); }
@SuppressWarnings({"unchecked"}) private PhysicalChannelConnection generateDirectBoundConnection(LogicalProducer producer, LogicalChannel channel, DeliveryType deliveryType, ClassLoader classLoader, PhysicalConnectionSource source) { LogicalBinding<?> binding = channel.getBinding(); ConnectionBindingGenerator bindingGenerator = getGenerator(binding); PhysicalConnectionTarget target = bindingGenerator.generateConnectionTarget(producer, binding, deliveryType); target.setClassLoader(classLoader); URI uri = channel.getUri(); Class<?> type = producer.getDefinition().getServiceContract().getInterfaceClass(); // the type is the producer interface since it is direct return new PhysicalChannelConnection(uri, producer.getUri(), source, target, type, true); }
@SuppressWarnings({"unchecked"}) public void disconnect(PhysicalChannelConnection physicalConnection) { PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); Key key = new Key(source.getSourceId(), target.getTargetId()); Holder holder = cachedConnections.get(key); if (holder == null) { return; } if (holder.count.decrementAndGet() == 0) { cachedConnections.remove(key); } else { return; } SourceConnectionAttacher sourceAttacher = sourceAttachers.get(source.getClass()); if (sourceAttacher == null) { throw new Fabric3Exception("Attacher not found for type: " + source.getClass().getName()); } TargetConnectionAttacher targetAttacher = targetAttachers.get(target.getClass()); if (targetAttacher == null) { throw new Fabric3Exception("Attacher not found for type: " + target.getClass().getName()); } sourceAttacher.detach(source, target); targetAttacher.detach(source, target); }
private void addTypeTransformer(PhysicalChannelConnection connection, EventStream stream, ClassLoader loader) { if (transformerHandlerFactory == null) { return; // bootstrap } List<DataType> sourceTypes = connection.getSource().getDataTypes(); List<DataType> targetTypes = connection.getTarget().getDataTypes(); if (sourceTypes.isEmpty() || targetTypes.isEmpty()) { return; } if (sourceTypes.size() > 1 || targetTypes.size() > 1) { // for now, only support one data type throw new Fabric3Exception("Multi-type events are not supported"); } DataType sourceType = sourceTypes.get(0); DataType targetType = targetTypes.get(0); if (sourceType.equals(targetType)) { return; } List<Class<?>> eventTypes = Collections.singletonList(stream.getEventType()); EventStreamHandler handler = transformerHandlerFactory.createHandler(sourceType, targetType, eventTypes, loader); stream.addHandler(handler); }
@SuppressWarnings({"unchecked"}) private PhysicalChannelConnection generateDirectBoundConnection(LogicalProducer producer, LogicalChannel channel, DeliveryType deliveryType, ClassLoader classLoader, PhysicalConnectionSource source) { LogicalBinding<?> binding = channel.getBinding(); ConnectionBindingGenerator bindingGenerator = getGenerator(binding); PhysicalConnectionTarget target = bindingGenerator.generateConnectionTarget(producer, binding, deliveryType); target.setClassLoader(classLoader); URI uri = channel.getUri(); Class<?> type = producer.getDefinition().getServiceContract().getInterfaceClass(); // the type is the producer interface since it is direct return new PhysicalChannelConnection(uri, producer.getUri(), source, target, type, true); }