Class AbstractVertxBasedMqttProtocolAdapter<T extends MqttProtocolAdapterProperties>
- java.lang.Object
-
- io.vertx.core.AbstractVerticle
-
- org.eclipse.hono.util.ConfigurationSupportingVerticle<T>
-
- org.eclipse.hono.service.AbstractServiceBase<T>
-
- org.eclipse.hono.adapter.AbstractProtocolAdapterBase<T>
-
- org.eclipse.hono.adapter.mqtt.AbstractVertxBasedMqttProtocolAdapter<T>
-
- Type Parameters:
T- The type of configuration properties this adapter supports/requires.
- All Implemented Interfaces:
io.vertx.core.Verticle,ProtocolAdapter,HealthCheckProvider
- Direct Known Subclasses:
VertxBasedMqttProtocolAdapter
public abstract class AbstractVertxBasedMqttProtocolAdapter<T extends MqttProtocolAdapterProperties> extends AbstractProtocolAdapterBase<T>
A base class for implementing Vert.x based Hono protocol adapters for publishing events & telemetry data using MQTT.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classAbstractVertxBasedMqttProtocolAdapter.MqttDeviceEndpointThe endpoint representing a connected MQTT device.
-
Field Summary
Fields Modifier and Type Field Description protected static intMEMORY_PER_CONNECTIONThe amount of memory (bytes) required for each connection.protected static intMINIMAL_MEMORY_JVMThe minimum amount of memory (bytes) that the adapter requires to run on a standard Java VM.protected static intMINIMAL_MEMORY_SUBSTRATEThe minimum amount of memory (bytes) that the adapter requires to run on a Substrate VM (i.e.-
Fields inherited from class org.eclipse.hono.adapter.AbstractProtocolAdapterBase
CONTENT_TYPE_OCTET_STREAM, KEY_MICROMETER_SAMPLE
-
Fields inherited from class org.eclipse.hono.service.AbstractServiceBase
log, tracer
-
-
Constructor Summary
Constructors Constructor Description AbstractVertxBasedMqttProtocolAdapter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected AuthHandler<MqttConnectContext>createAuthHandler()Creates the default auth handler to use for authenticating devices.protected voidcustomizeDownstreamMessageProperties(Map<String,Object> messageProperties, MqttContext ctx)Invoked before the message is sent to the downstream peer.protected voiddoStart(io.vertx.core.Promise<Void> startPromise)protected voiddoStop(io.vertx.core.Promise<Void> stopPromise)protected intgetActualInsecurePort()protected intgetActualPort()protected io.vertx.core.Future<io.vertx.core.buffer.Buffer>getCommandPayload(org.eclipse.hono.client.command.CommandContext ctx)Get the buffer to send as a command to the gateway/device.intgetInsecurePortDefaultValue()protected MqttAdapterMetricsgetMetrics()Gets the metrics for this service.intgetPortDefaultValue()protected io.vertx.core.Future<Void>handleBeforeCredentialsValidation(DeviceCredentials credentials, MqttConnectContext executionContext)Handles any operations that should be invoked as part of the authentication process after the credentials got determined and before they get validated.protected voidonClose(io.vertx.mqtt.MqttEndpoint endpoint)Invoked before the connection with a device is closed.protected voidonMessageSent(MqttContext ctx)Invoked when a message has been forwarded downstream successfully.protected voidonMessageUndeliverable(MqttContext ctx)Invoked when a message could not be forwarded downstream.protected abstract io.vertx.core.Future<Void>onPublishedMessage(MqttContext ctx)Processes an MQTT message that has been published by a device.voidsetAuthHandler(AuthHandler<MqttConnectContext> authHandler)Sets the authentication handler to use for authenticating devices.voidsetMetrics(MqttAdapterMetrics metrics)Sets the metrics for this service.voidsetMqttInsecureServer(io.vertx.mqtt.MqttServer server)Sets the MQTT server to use for handling non-secure MQTT connections.voidsetMqttSecureServer(io.vertx.mqtt.MqttServer server)Sets the MQTT server to use for handling secure MQTT connections.io.vertx.core.Future<Void>uploadCommandResponseMessage(MqttContext ctx, org.eclipse.hono.util.ResourceIdentifier targetAddress)Uploads a command response message.io.vertx.core.Future<Void>uploadEventMessage(MqttContext ctx, String tenant, String deviceId, io.vertx.core.buffer.Buffer payload)Forwards an event to the AMQP Messaging Network.io.vertx.core.Future<Void>uploadMessage(MqttContext ctx, org.eclipse.hono.util.ResourceIdentifier resource, io.vertx.mqtt.messages.MqttPublishMessage message)Forwards a message to the AMQP Messaging Network.io.vertx.core.Future<Void>uploadTelemetryMessage(MqttContext ctx, String tenant, String deviceId, io.vertx.core.buffer.Buffer payload)Forwards a telemetry message to the AMQP Messaging Network.-
Methods inherited from class org.eclipse.hono.adapter.AbstractProtocolAdapterBase
addMicrometerSample, checkConnectionDurationLimit, checkConnectionLimit, checkDeviceRegistration, checkMessageLimit, getCommandConsumerFactory, getCommandResponseSender, getConnectionEventProducer, getConnectionLimitManager, getCredentialsClient, getDownstreamMessageProperties, getEventSender, getMicrometerSample, getOutcome, getRegistrationAssertion, getRegistrationClient, getResourceLimitChecks, getServerTrustOptions, getTelemetrySender, getTenantClient, getTenantConfiguration, isAdapterEnabled, isPayloadOfIndicatedType, isTerminalError, registerEventLoopBlockedCheck, registerLivenessChecks, registerReadinessChecks, sendCommandResponse, sendConnectedEvent, sendConnectedTtdEvent, sendDisconnectedEvent, sendDisconnectedTtdEvent, sendTtdEvent, setCommandConsumerFactory, setCommandRouterClient, setConfig, setConnectionEventProducer, setConnectionLimitManager, setCredentialsClient, setMessagingClientProviders, setRegistrationClient, setResourceLimitChecks, setTenantClient, startInternal, startServiceClient, stopInternal, stopServiceClient, updateLastGateway, updateLastGateway, validateAddress
-
Methods inherited from class org.eclipse.hono.service.AbstractServiceBase
addTlsKeyCertOptions, addTlsTrustOptions, checkPortConfiguration, determineInsecurePort, determineSecurePort, getBindAddress, getInsecurePort, getInsecurePortBindAddress, getPort, isInsecurePortEnabled, isSecurePortEnabled, setHealthCheckServer, setTracer, start, stop
-
Methods inherited from class org.eclipse.hono.util.ConfigurationSupportingVerticle
getConfig, setSpecificConfig
-
Methods inherited from class io.vertx.core.AbstractVerticle
config, deploymentID, getVertx, init, processArgs, start, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.eclipse.hono.adapter.ProtocolAdapter
getTimeUntilDisconnect, getTypeName
-
-
-
-
Field Detail
-
MINIMAL_MEMORY_JVM
protected static final int MINIMAL_MEMORY_JVM
The minimum amount of memory (bytes) that the adapter requires to run on a standard Java VM.- See Also:
- Constant Field Values
-
MINIMAL_MEMORY_SUBSTRATE
protected static final int MINIMAL_MEMORY_SUBSTRATE
The minimum amount of memory (bytes) that the adapter requires to run on a Substrate VM (i.e. when running as a GraalVM native image).- See Also:
- Constant Field Values
-
MEMORY_PER_CONNECTION
protected static final int MEMORY_PER_CONNECTION
The amount of memory (bytes) required for each connection.- See Also:
- Constant Field Values
-
-
Method Detail
-
setAuthHandler
public final void setAuthHandler(AuthHandler<MqttConnectContext> authHandler)
Sets the authentication handler to use for authenticating devices.- Parameters:
authHandler- The handler to use.- Throws:
NullPointerException- if handler isnull.
-
getPortDefaultValue
public int getPortDefaultValue()
- Specified by:
getPortDefaultValuein classAbstractServiceBase<T extends MqttProtocolAdapterProperties>
-
getInsecurePortDefaultValue
public int getInsecurePortDefaultValue()
- Specified by:
getInsecurePortDefaultValuein classAbstractServiceBase<T extends MqttProtocolAdapterProperties>
-
getActualPort
protected final int getActualPort()
- Specified by:
getActualPortin classAbstractServiceBase<T extends MqttProtocolAdapterProperties>
-
getActualInsecurePort
protected final int getActualInsecurePort()
- Specified by:
getActualInsecurePortin classAbstractServiceBase<T extends MqttProtocolAdapterProperties>
-
setMetrics
public final void setMetrics(MqttAdapterMetrics metrics)
Sets the metrics for this service.- Parameters:
metrics- The metrics
-
getMetrics
protected final MqttAdapterMetrics getMetrics()
Gets the metrics for this service.- Returns:
- The metrics
-
createAuthHandler
protected AuthHandler<MqttConnectContext> createAuthHandler()
Creates the default auth handler to use for authenticating devices.This default implementation creates a
ChainAuthHandlerconsisting of anX509AuthHandlerand aConnectPacketAuthHandlerinstance.Subclasses may either set the auth handler explicitly using
setAuthHandler(AuthHandler)or override this method in order to create a custom auth handler.- Returns:
- The handler.
-
handleBeforeCredentialsValidation
protected io.vertx.core.Future<Void> handleBeforeCredentialsValidation(DeviceCredentials credentials, MqttConnectContext executionContext)
Handles any operations that should be invoked as part of the authentication process after the credentials got determined and before they get validated. Can be used to perform checks using the credentials and tenant information before the potentially expensive credentials validation is doneThe default implementation updates the trace sampling priority in the execution context tracing span. It also verifies that the tenant provided via the credentials is enabled and that the adapter is enabled for that tenant, failing the returned future if either is not the case.
Subclasses should override this method in order to perform additional operations after calling this super method.
- Parameters:
credentials- The credentials.executionContext- The execution context, including the TenantObject.- Returns:
- A future indicating the outcome of the operation. A failed future will fail the authentication attempt.
-
setMqttSecureServer
public void setMqttSecureServer(io.vertx.mqtt.MqttServer server)
Sets the MQTT server to use for handling secure MQTT connections.- Parameters:
server- The server.- Throws:
NullPointerException- if the server isnull.IllegalStateException- if the adapter has already been started.
-
setMqttInsecureServer
public void setMqttInsecureServer(io.vertx.mqtt.MqttServer server)
Sets the MQTT server to use for handling non-secure MQTT connections.- Parameters:
server- The server.- Throws:
NullPointerException- if the server isnull.IllegalStateException- if the adapter has already been started.
-
doStart
protected final void doStart(io.vertx.core.Promise<Void> startPromise)
- Overrides:
doStartin classAbstractProtocolAdapterBase<T extends MqttProtocolAdapterProperties>
-
doStop
protected final void doStop(io.vertx.core.Promise<Void> stopPromise)
- Overrides:
doStopin classAbstractProtocolAdapterBase<T extends MqttProtocolAdapterProperties>
-
uploadMessage
public final io.vertx.core.Future<Void> uploadMessage(MqttContext ctx, org.eclipse.hono.util.ResourceIdentifier resource, io.vertx.mqtt.messages.MqttPublishMessage message)
Forwards a message to the AMQP Messaging Network.- Parameters:
ctx- The context in which the MQTT message has been published.resource- The resource that the message should be forwarded to.message- The message to send.- Returns:
- A future indicating the outcome of the operation.
The future will succeed if the message has been forwarded successfully. Otherwise the future will fail with a
ServiceInvocationException. - Throws:
NullPointerException- if any of context, resource or payload isnull.IllegalArgumentException- if the payload is empty.
-
uploadTelemetryMessage
public final io.vertx.core.Future<Void> uploadTelemetryMessage(MqttContext ctx, String tenant, String deviceId, io.vertx.core.buffer.Buffer payload)
Forwards a telemetry message to the AMQP Messaging Network.- Parameters:
ctx- The context in which the MQTT message has been published.tenant- The tenant of the device that has produced the data.deviceId- The id of the device that has produced the data.payload- The message payload to send.- Returns:
- A future indicating the outcome of the operation.
The future will succeed if the message has been forwarded successfully. Otherwise the future will fail with a
ServiceInvocationException. - Throws:
NullPointerException- if any of context, tenant, device ID or payload isnull.IllegalArgumentException- if the context does not contain a telemetry message or if the payload is empty.
-
uploadEventMessage
public final io.vertx.core.Future<Void> uploadEventMessage(MqttContext ctx, String tenant, String deviceId, io.vertx.core.buffer.Buffer payload)
Forwards an event to the AMQP Messaging Network.- Parameters:
ctx- The context in which the MQTT message has been published.tenant- The tenant of the device that has produced the data.deviceId- The id of the device that has produced the data.payload- The message payload to send.- Returns:
- A future indicating the outcome of the operation.
The future will succeed if the message has been forwarded successfully. Otherwise the future will fail with a
ServiceInvocationException. - Throws:
NullPointerException- if any of context, tenant, device ID or payload isnull.IllegalArgumentException- if the context does not contain an event or if the payload is empty.
-
uploadCommandResponseMessage
public final io.vertx.core.Future<Void> uploadCommandResponseMessage(MqttContext ctx, org.eclipse.hono.util.ResourceIdentifier targetAddress)
Uploads a command response message.- Parameters:
ctx- The context in which the MQTT message has been published.targetAddress- The address that the response should be forwarded to.- Returns:
- A future indicating the outcome of the operation.
The future will succeed if the message has been uploaded successfully. Otherwise, the future will fail with a
ServiceInvocationException. - Throws:
NullPointerException- if any of the parameters arenull.
-
onClose
protected void onClose(io.vertx.mqtt.MqttEndpoint endpoint)
Invoked before the connection with a device is closed.Subclasses should override this method in order to release any device specific resources.
This default implementation does nothing.
- Parameters:
endpoint- The connection to be closed.
-
onPublishedMessage
protected abstract io.vertx.core.Future<Void> onPublishedMessage(MqttContext ctx)
Processes an MQTT message that has been published by a device.Subclasses should determine
- the tenant and identifier of the device that has published the message
- the payload to send downstream
- the content type of the payload
- Parameters:
ctx- The context in which the MQTT message has been published. TheMqttContext.topic()method will return a non-null resource identifier for the topic that the message has been published to.- Returns:
- A future indicating the outcome of the operation.
The future will succeed if the message has been successfully uploaded. Otherwise, the future will fail with a
ServiceInvocationException.
-
customizeDownstreamMessageProperties
protected void customizeDownstreamMessageProperties(Map<String,Object> messageProperties, MqttContext ctx)
Invoked before the message is sent to the downstream peer.Subclasses may override this method in order to customize the properties used for sending the message, e.g. adding custom properties.
- Parameters:
messageProperties- The properties that are being added to the downstream message.ctx- The message processing context.
-
onMessageSent
protected void onMessageSent(MqttContext ctx)
Invoked when a message has been forwarded downstream successfully.This default implementation does nothing.
Subclasses should override this method in order to e.g. update metrics counters.
- Parameters:
ctx- The context in which the MQTT message has been published.
-
onMessageUndeliverable
protected void onMessageUndeliverable(MqttContext ctx)
Invoked when a message could not be forwarded downstream.This method will only be invoked if the failure to forward the message has not been caused by the device that published the message. In particular, this method will not be invoked for messages that cannot be authorized or that are published to an unsupported/unrecognized topic. Such messages will be silently discarded.
This default implementation does nothing.
Subclasses should override this method in order to e.g. update metrics counters.
- Parameters:
ctx- The context in which the MQTT message has been published.
-
getCommandPayload
protected io.vertx.core.Future<io.vertx.core.buffer.Buffer> getCommandPayload(org.eclipse.hono.client.command.CommandContext ctx)
Get the buffer to send as a command to the gateway/device.Subclasses should override this method in order to overwrite the provided command.
- Parameters:
ctx- The command context for this command.- Returns:
- A future containing the mapped buffer.
-
-