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); }
/** * Returns an id for the connection target. Connections may be cached and reused if their source and target ids match. * * Bindings will override the default method if the connection from a transport to a channel should be re-used (i.e. guarantee only one is created per * runtime). * * @return the id */ public String getSourceId() { return getUri().toString() + "_source_" + this.getClass().getName() + "#"+ getTopic(); }
private PhysicalChannelConnection generateLocalConnection(LogicalConsumer consumer, LogicalChannel channel, PhysicalConnectionTarget target, ClassLoader classLoader) { // the channel does not have bindings, which means it is a local channel // construct a local connection to the channel URI uri = channel.getUri(); PhysicalConnectionSource source = new ChannelSource(uri, ChannelSide.CONSUMER); source.setSequence(consumer.getDefinition().getSequence()); source.setClassLoader(classLoader); Class<?> type = getType(consumer); boolean bound = channel.isBound(); return new PhysicalChannelConnection(uri, consumer.getUri(), source, target, type, bound); }
PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); if (source.isDirectConnection() || target.isDirectConnection()) { int sequence = source.getSequence(); String topic = physicalConnection.getTopic(); URI channelUri = physicalConnection.getChannelUri(); if (physicalConnection.isBound()) { Class<?> type = source.isDirectConnection() ? source.getServiceInterface() : target.getServiceInterface(); DirectConnectionFactory factory = connectionFactories.get(type); if (factory == null) { addTypeTransformer(physicalConnection, stream, loader); String topic = physicalConnection.getTopic(); int sequence = source.getSequence(); return new ChannelConnectionImpl(stream, topic, sequence);
@SuppressWarnings({"unchecked"}) public List<PhysicalChannelConnection> generateProducer(LogicalProducer producer, Map<LogicalChannel, DeliveryType> channels) { LogicalComponent<?> component = producer.getParent(); ComponentGenerator<?> componentGenerator = getGenerator(component); PhysicalConnectionSource source = componentGenerator.generateConnectionSource(producer); URI classLoaderId = component.getDefinition().getContributionUri(); ClassLoader classLoader = classLoaderRegistry.getClassLoader(classLoaderId); source.setClassLoader(classLoader); if (isDirect(producer, channels)) { source.setDirectConnection(true); return generateDirectConnections(producer, channels, source, classLoader); } else { return generateConnections(producer, channels, source, classLoader); } }
public void attach(PhysicalConnectionSource source, ChannelTarget target, ChannelConnection connection) { if (source.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = target.getUri(); Channel channel = getChannel(uri, target.getChannelSide()); channel.attach(connection); connection.setCloseable(() -> { // no-op }); }
public <T> T getConsumer(Class<T> interfaze, String name, String topic) { LogicalChannel logicalChannel = getChannel(name); LogicalConsumer consumer = createConsumer(interfaze, 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(consumer, channels); PhysicalChannelConnection connection = connections.get(0); // safe as there is only one connection connection.setTopic(topic); connection.getSource().setTopic(topic); connection.getTarget().setTopic(topic); channelConnector.connect(connection); NonManagedConnectionTarget target = (NonManagedConnectionTarget) connection.getTarget(); return interfaze.cast(target.getProxy()); }
public void detach(PhysicalConnectionSource source, ZeroMQConnectionTarget target) { ZeroMQMetadata metadata = target.getMetadata(); String connectionId = source.getUri().toString(); broker.release(connectionId, metadata); }
@SuppressWarnings({"unchecked"}) private PhysicalChannelConnection generateDirectBoundConnection(LogicalConsumer consumer, LogicalChannel channel, DeliveryType deliveryType, ClassLoader classLoader, PhysicalConnectionTarget target) { LogicalBinding<?> binding = channel.getBinding(); ConnectionBindingGenerator bindingGenerator = getGenerator(binding); PhysicalConnectionSource source = bindingGenerator.generateConnectionSource(consumer, binding, deliveryType); source.setClassLoader(classLoader); URI uri = channel.getUri(); Class<?> type = getType(consumer); return new PhysicalChannelConnection(uri, consumer.getUri(), source, target, type, true); }
PhysicalConnectionSource source = physicalConnection.getSource(); PhysicalConnectionTarget target = physicalConnection.getTarget(); if (source.isDirectConnection() || target.isDirectConnection()) { int sequence = source.getSequence(); String topic = physicalConnection.getTopic(); URI channelUri = physicalConnection.getChannelUri(); if (physicalConnection.isBound()) { Class<?> type = source.isDirectConnection() ? source.getServiceInterface() : target.getServiceInterface(); DirectConnectionFactory factory = connectionFactories.get(type); if (factory == null) { addTypeTransformer(physicalConnection, stream, loader); String topic = physicalConnection.getTopic(); int sequence = source.getSequence(); return new ChannelConnectionImpl(stream, topic, sequence);
private PhysicalChannelConnection generateLocalConnection(LogicalConsumer consumer, LogicalChannel channel, PhysicalConnectionTarget target, ClassLoader classLoader) { // the channel does not have bindings, which means it is a local channel // construct a local connection to the channel URI uri = channel.getUri(); PhysicalConnectionSource source = new ChannelSource(uri, ChannelSide.CONSUMER); source.setSequence(consumer.getDefinition().getSequence()); source.setClassLoader(classLoader); Class<?> type = getType(consumer); boolean bound = channel.isBound(); return new PhysicalChannelConnection(uri, consumer.getUri(), source, target, type, bound); }
@SuppressWarnings({"unchecked"}) public List<PhysicalChannelConnection> generateProducer(LogicalProducer producer, Map<LogicalChannel, DeliveryType> channels) { LogicalComponent<?> component = producer.getParent(); ComponentGenerator<?> componentGenerator = getGenerator(component); PhysicalConnectionSource source = componentGenerator.generateConnectionSource(producer); URI classLoaderId = component.getDefinition().getContributionUri(); ClassLoader classLoader = classLoaderRegistry.getClassLoader(classLoaderId); source.setClassLoader(classLoader); if (isDirect(producer, channels)) { source.setDirectConnection(true); return generateDirectConnections(producer, channels, source, classLoader); } else { return generateConnections(producer, channels, source, classLoader); } }
public void attach(PhysicalConnectionSource source, ChannelTarget target, ChannelConnection connection) { if (source.isDirectConnection()) { // no event stream to attach since this is a direct connection return; } URI uri = target.getUri(); Channel channel = getChannel(uri, target.getChannelSide()); channel.attach(connection); connection.setCloseable(() -> { // no-op }); }
public <T> T getConsumer(Class<T> interfaze, String name, String topic) { LogicalChannel logicalChannel = getChannel(name); LogicalConsumer consumer = createConsumer(interfaze, 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(consumer, channels); PhysicalChannelConnection connection = connections.get(0); // safe as there is only one connection connection.setTopic(topic); connection.getSource().setTopic(topic); connection.getTarget().setTopic(topic); channelConnector.connect(connection); NonManagedConnectionTarget target = (NonManagedConnectionTarget) connection.getTarget(); return interfaze.cast(target.getProxy()); }
public void attach(PhysicalConnectionSource source, ZeroMQConnectionTarget target, ChannelConnection connection) { ZeroMQMetadata metadata = target.getMetadata(); String connectionId = source.getUri().toString(); ClassLoader loader = target.getClassLoader(); boolean dedicatedThread = target.isDedicatedThread(); broker.connect(connectionId, metadata, dedicatedThread, connection, loader); }
@SuppressWarnings({"unchecked"}) private PhysicalChannelConnection generateDirectBoundConnection(LogicalConsumer consumer, LogicalChannel channel, DeliveryType deliveryType, ClassLoader classLoader, PhysicalConnectionTarget target) { LogicalBinding<?> binding = channel.getBinding(); ConnectionBindingGenerator bindingGenerator = getGenerator(binding); PhysicalConnectionSource source = bindingGenerator.generateConnectionSource(consumer, binding, deliveryType); source.setClassLoader(classLoader); URI uri = channel.getUri(); Class<?> type = getType(consumer); return new PhysicalChannelConnection(uri, consumer.getUri(), source, target, type, true); }
@SuppressWarnings({"unchecked"}) private PhysicalChannelConnection generateBoundConnection(LogicalConsumer consumer, LogicalChannel channel, DeliveryType deliveryType, ClassLoader classLoader) { // use the bindings on the channel to create a consumer binding configuration LogicalBinding<?> binding = channel.getBinding(); ConnectionBindingGenerator bindingGenerator = getGenerator(binding); PhysicalConnectionSource source = bindingGenerator.generateConnectionSource(consumer, binding, deliveryType); source.setSequence(consumer.getDefinition().getSequence()); source.setClassLoader(classLoader); URI uri = channel.getUri(); ChannelTarget target = new ChannelTarget(uri, ChannelSide.CONSUMER); target.setClassLoader(classLoader); Class<?> type = getType(consumer); return new PhysicalChannelConnection(uri, consumer.getUri(), source, target, type, true); }
@SuppressWarnings("unchecked") public void attach(PhysicalConnectionSource source, NATSConnectionTarget target, ChannelConnection connection) { Nats nats = connectionManager.getNats(target); if (!source.isDirectConnection()) { String topic = target.getTopic() != null ? target.getTopic() : target.getDefaultTopic(); String serializerName = target.getData().getSerializer(); Function<Object, String> serializer = serializerName != null ? InstanceResolver.getInstance(serializerName, info, cm) : null; connection.getEventStream().addHandler((event, batch) -> { String serialized = serializer != null ? serializer.apply(event) : event.toString(); nats.publish(topic, serialized); }); connection.setCloseable(() -> connectionManager.release(target.getChannelUri(), topic)); } }
private NonManagedConnectionSource connect(String topic, List<PhysicalChannelConnection> connections) { NonManagedConnectionSource source = null; for (PhysicalChannelConnection connection : connections) { connection.setTopic(topic); connection.getSource().setTopic(topic); connection.getTarget().setTopic(topic); channelConnector.connect(connection); if (connection.getSource() instanceof NonManagedConnectionSource) { source = (NonManagedConnectionSource) connection.getSource(); } } if (source == null) { throw new Fabric3Exception("NonManagedConnectionSource not found publishing to topic: " + topic); } return source; }