Table of Contents
This section illustrates using Qpid specific extensions to JMS for the management of connections, queues, exchanges and bindings.
It is not recommended that these extensions are generally used. These interfaces are subject to change and will not be supported in this form for AMQP 1.0. Instead, the reader is directed towards the Managment interfaces of the Broker.
Connection extensions allows overriding connection configurations like username or password in response to some environment changes like account rotation or authentication token expiration.
The extensions take the form of a BiFunction<Connection, URI, Object> passed into the ConnectionFactory using the AMQConnectionFactory#setExtension(String, BiFunction) or PooledConnectionFactory#setExtension(String, BiFunction).
A table below lists supported extensions.
Table C.1. Connection Extensions
Extension Name | Description |
---|---|
username | Allows to hook a custom code for provisioning of user name which would be used in authentication with a remote host. |
password | Allows to hook a custom code for provisioning of user password which would be used in authentication with a remote host. |
The following example illustrates how expired OAUTH2 authentication token can be recreated.
Example C.1. Inject password extension
final String connectionURL = "..."; final TokenGenerator tokenGenerator = new TokenGenerator(...); final BiFunction<Connection, URI, Object> tokenExtension = new BiFunction<Connection, URI, Object>() { private volatile String token; private long currentTokenExpirationTime; @Override public Object apply(final Connection connection, final URI uri) { long currentTime = System.currentTimeMillis(); if (currentTime > currentTokenExpirationTime) { this.token = tokenGenerator.generateAccessToken(); this.currentTokenExpirationTime = tokenGenerator.getTokenExpirationTime(token); } return this.token; } }; final AMQConnectionFactory factory = new AMQConnectionFactory(connectionURL); factory.setExtension(ConnectionExtension.PASSWORD_OVERRIDE.name(), tokenExtension); final Connection connection = factory.createConnection();
Connection URL | |
Helper object to generate access token for a specific OAUTH2 implementation | |
Password extension for token renewal | |
Check token expiration | |
Get new token | |
Preserve token expiration time | |
Create connection factory | |
Register password extension for token regeneration | |
Open connection |
In the snippet above an implementation of BiFunction<Connection, URI, Object> is created at (3) for access token provisioning. The function implementation checks the token expiration at (4) and regenerate the token at (5) using a helper object (2) implementing calls to OAUTH2 specific API. The token expiration time is preserved at (6) for the following reconnects attempts. An instance of AMQConnectionFactory is created at (7) for a given connection URL (1). A password extension is registered at (8). JMS connection is open at (9). The example uses a hypothetical class TokenGenerator invoking underlying OAUTH2 API to generate/renew access token and get token expiration time.
Connection Extensions can be used together with ConnectAttemptListener to change credentials on connectivity failures. Please check Section C.2, “ConnectAttemptListener” for more details.
Apache Qpid, Messaging built on AMQP; Copyright © 2015 The Apache Software Foundation; Licensed under the Apache License, Version 2.0; Apache Qpid, Qpid, Qpid Proton, Proton, Apache, the Apache feather logo, and the Apache Qpid project logo are trademarks of The Apache Software Foundation; All other marks mentioned may be trademarks or registered trademarks of their respective owners