/** * @return whether this client is consuming at all */ protected final boolean isConsuming() { return !connection().getSources().isEmpty(); }
/** * @return the sources configured for this connection or an empty set if no sources were configured. */ protected final List<Source> getSourcesOrEmptySet() { return connection().getSources(); }
/** * @return whether this client is consuming at all */ protected final boolean isConsuming() { return !connection().getSources().isEmpty(); }
/** * @return the sources configured for this connection or an empty set if no sources were configured. */ protected final List<Source> getSourcesOrEmptySet() { return connection().getSources(); }
private static void validateAddresses(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources() .stream() .flatMap(source -> source.getAddresses().stream()) .forEach(a -> validateAddress(a, true, dittoHeaders)); // no wildcards allowed for publish targets connection.getTargets() .stream() .map(Target::getAddress) .forEach(a -> validateAddress(a, false, dittoHeaders)); }
/** * Validate protocol-specific configurations of sources. * * @param connection the connection to check. * @param dittoHeaders headers of the command that triggered the connection validation. */ protected void validateSourceConfigs(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources().forEach(source -> validateSource(source, dittoHeaders, sourceDescription(source, connection))); }
private static void validateAddresses(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources() .stream() .flatMap(source -> source.getAddresses().stream()) .forEach(a -> validateAddress(a, true, dittoHeaders)); // no wildcards allowed for publish targets connection.getTargets() .stream() .map(Target::getAddress) .forEach(a -> validateAddress(a, false, dittoHeaders)); }
/** * Validate protocol-specific configurations of sources. * * @param connection the connection to check. * @param dittoHeaders headers of the command that triggered the connection validation. */ protected void validateSourceConfigs(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources().forEach(source -> validateSource(source, dittoHeaders, sourceDescription(source, connection))); }
/** * Uses the given session to create the specified count of message consumers for every sources addresses. * * @param session the session * @return the consumers * @throws org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionFailedException if creation of one * or more consumers failed */ private List<ConsumerData> createConsumers(final Session session) { final Map<String, Exception> failedSources = new HashMap<>(); final List<ConsumerData> consumers = connection.getSources().stream().flatMap(source -> source.getAddresses().stream().flatMap(sourceAddress -> IntStream.range(0, source.getConsumerCount()) .mapToObj(i -> sourceAddress + "-" + i) .map(addressWithIndex -> createJmsConsumer(session, failedSources, source, sourceAddress, addressWithIndex)) .filter(Objects::nonNull) .collect(Collectors.toList()).stream() ).collect(Collectors.toList()).stream() ).collect(Collectors.toList()); if (!failedSources.isEmpty()) { throw buildConnectionFailedException(failedSources); } return consumers; }
/** * Uses the given session to create the specified count of message consumers for every sources addresses. * * @param session the session * @return the consumers * @throws org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionFailedException if creation of one * or more consumers failed */ private List<ConsumerData> createConsumers(final Session session) { final Map<String, Exception> failedSources = new HashMap<>(); final List<ConsumerData> consumers = connection.getSources().stream().flatMap(source -> source.getAddresses().stream().flatMap(sourceAddress -> IntStream.range(0, source.getConsumerCount()) .mapToObj(i -> sourceAddress + "-" + i) .map(addressWithIndex -> createJmsConsumer(session, failedSources, source, sourceAddress, addressWithIndex)) .filter(Objects::nonNull) .collect(Collectors.toList()).stream() ).collect(Collectors.toList()).stream() ).collect(Collectors.toList()); if (!failedSources.isEmpty()) { throw buildConnectionFailedException(failedSources); } return consumers; }
private void validateSourceAndTargetAddressesAreNonempty(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources().forEach(source -> { if (source.getAddresses().isEmpty() || source.getAddresses().contains("")) { final String location = String.format("Source %d of connection <%s>", source.getIndex(), connection.getId()); throw emptyAddressesError(location, dittoHeaders); } }); connection.getTargets().forEach(target -> { if (target.getAddress().isEmpty()) { final String location = String.format("Targets of connection <%s>", connection.getId()); throw emptyAddressesError(location, dittoHeaders); } target.getTopics().forEach(topic -> topic.getFilter().ifPresent(filter -> { // will throw an InvalidRqlExpressionException if the RQL expression was not valid: queryFilterCriteriaFactory.filterCriteria(filter, dittoHeaders); })); }); }
private void validateSourceAndTargetAddressesAreNonempty(final Connection connection, final DittoHeaders dittoHeaders) { connection.getSources().forEach(source -> { if (source.getAddresses().isEmpty() || source.getAddresses().contains("")) { final String location = String.format("Source %d of connection <%s>", source.getIndex(), connection.getId()); throw emptyAddressesError(location, dittoHeaders); } }); connection.getTargets().forEach(target -> { if (target.getAddress().isEmpty()) { final String location = String.format("Targets of connection <%s>", connection.getId()); throw emptyAddressesError(location, dittoHeaders); } target.getTopics().forEach(topic -> topic.getFilter().ifPresent(filter -> { // will throw an InvalidRqlExpressionException if the RQL expression was not valid: queryFilterCriteriaFactory.filterCriteria(filter, dittoHeaders); })); }); }
@Test public void createMinimalConnectionConfigurationInstance() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .sources(SOURCES) .targets(TARGETS) .build(); assertThat(connection.getId()).isEqualTo(ID); assertThat((Object) connection.getConnectionType()).isEqualTo(TYPE); assertThat(connection.getUri()).isEqualTo(URI); assertThat(connection.getSources()).isEqualTo(SOURCES); }
/** * Start MQTT publisher and subscribers, expect "Status.Success" from each of them, then send "ClientConnected" to * self. * * @param connection connection of the publisher and subscribers. * @param dryRun if set to true, exchange no message between the broker and the Ditto cluster. */ private void connectClient(final Connection connection, final boolean dryRun) { final MqttConnectionFactory factory = connectionFactoryCreator.apply(connection, stateData().getSessionHeaders()); // start publisher startMqttPublisher(factory, dryRun); // start consumers if (isConsuming()) { // start message mapping processor actor early so that consumer streams can be run final Either<DittoRuntimeException, ActorRef> messageMappingProcessor = startMessageMappingProcessor(); if (messageMappingProcessor.isLeft()) { final DittoRuntimeException e = messageMappingProcessor.left().get(); log.warning("failed to start mapping processor due to {}", e); } else { final ActorRef mappingActor = messageMappingProcessor.right().get(); // start new KillSwitch for the next batch of consumers refreshConsumerKillSwitch(KillSwitches.shared("consumerKillSwitch")); connection().getSources().forEach(source -> startMqttConsumers(factory, mappingActor, source, dryRun)); } } else { log.info("Not starting consumption because there is no source."); } }
/** * Start MQTT publisher and subscribers, expect "Status.Success" from each of them, then send "ClientConnected" to * self. * * @param connection connection of the publisher and subscribers. * @param dryRun if set to true, exchange no message between the broker and the Ditto cluster. */ private void connectClient(final Connection connection, final boolean dryRun) { final MqttConnectionFactory factory = connectionFactoryCreator.apply(connection, stateData().getSessionHeaders()); // start publisher startMqttPublisher(factory, dryRun); // start consumers if (isConsuming()) { // start message mapping processor actor early so that consumer streams can be run final Either<DittoRuntimeException, ActorRef> messageMappingProcessor = startMessageMappingProcessor(); if (messageMappingProcessor.isLeft()) { final DittoRuntimeException e = messageMappingProcessor.left().get(); log.warning("failed to start mapping processor due to {}", e); } else { final ActorRef mappingActor = messageMappingProcessor.right().get(); // start new KillSwitch for the next batch of consumers refreshConsumerKillSwitch(KillSwitches.shared("consumerKillSwitch")); connection().getSources().forEach(source -> startMqttConsumers(factory, mappingActor, source, dryRun)); } } else { log.info("Not starting consumption because there is no source."); } }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }