2014-12-11 07:17:29 -05:00
|
|
|
# Intercepting Operations
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Apache ActiveMQ Artemis supports *interceptors* to intercept packets entering
|
|
|
|
and exiting the server. Incoming and outgoing interceptors are be called for
|
|
|
|
any packet entering or exiting the server respectively. This allows custom code
|
|
|
|
to be executed, e.g. for auditing packets, filtering or other reasons.
|
|
|
|
Interceptors can change the packets they intercept. This makes interceptors
|
|
|
|
powerful, but also potentially dangerous.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Implementing The Interceptors
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2016-07-03 22:30:13 -04:00
|
|
|
All interceptors are protocol specific.
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
An interceptor for the core protocol must implement the interface
|
|
|
|
`Interceptor`:
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```java
|
2017-07-31 06:26:28 -04:00
|
|
|
package org.apache.activemq.artemis.api.core.interceptor;
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
public interface Interceptor
|
2015-02-25 08:37:19 -05:00
|
|
|
{
|
2014-12-11 07:17:29 -05:00
|
|
|
boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException;
|
|
|
|
}
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2016-07-03 22:30:13 -04:00
|
|
|
For stomp protocol an interceptor must implement the interface `StompFrameInterceptor`:
|
2015-03-13 05:29:33 -04:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```java
|
2015-04-27 17:32:30 -04:00
|
|
|
package org.apache.activemq.artemis.core.protocol.stomp;
|
2015-03-13 05:29:33 -04:00
|
|
|
|
2016-07-03 22:30:13 -04:00
|
|
|
public interface StompFrameInterceptor extends BaseInterceptor<StompFrame>
|
|
|
|
{
|
|
|
|
boolean intercept(StompFrame stompFrame, RemotingConnection connection);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Likewise for MQTT protocol, an interceptor must implement the interface
|
|
|
|
`MQTTInterceptor`:
|
2016-07-03 22:30:13 -04:00
|
|
|
|
2019-03-30 12:52:29 -04:00
|
|
|
```java
|
|
|
|
package org.apache.activemq.artemis.core.protocol.mqtt;
|
2016-07-03 22:30:13 -04:00
|
|
|
|
|
|
|
public interface MQTTInterceptor extends BaseInterceptor<MqttMessage>
|
2015-03-13 05:29:33 -04:00
|
|
|
{
|
2016-07-03 22:30:13 -04:00
|
|
|
boolean intercept(MqttMessage mqttMessage, RemotingConnection connection);
|
2015-03-13 05:29:33 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-12-04 10:25:29 -05:00
|
|
|
The returned boolean value is important:
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
- if `true` is returned, the process continues normally
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
- if `false` is returned, the process is aborted, no other interceptors will be
|
|
|
|
called and the packet will not be processed further by the server.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Configuring The Interceptors
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Both incoming and outgoing interceptors are configured in `broker.xml`:
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```xml
|
|
|
|
<remoting-incoming-interceptors>
|
|
|
|
<class-name>org.apache.activemq.artemis.jms.example.LoginInterceptor</class-name>
|
|
|
|
<class-name>org.apache.activemq.artemis.jms.example.AdditionalPropertyInterceptor</class-name>
|
|
|
|
</remoting-incoming-interceptors>
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
<remoting-outgoing-interceptors>
|
|
|
|
<class-name>org.apache.activemq.artemis.jms.example.LogoutInterceptor</class-name>
|
|
|
|
<class-name>org.apache.activemq.artemis.jms.example.AdditionalPropertyInterceptor</class-name>
|
|
|
|
</remoting-outgoing-interceptors>
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
See the documentation on [adding runtime dependencies](using-server.md) to
|
2018-01-22 15:47:53 -05:00
|
|
|
understand how to make your interceptor available to the broker.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Interceptors on the Client Side
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
The interceptors can also be run on the Apache ActiveMQ Artemit client side to
|
|
|
|
intercept packets either sent by the client to the server or by the server to
|
|
|
|
the client. This is done by adding the interceptor to the `ServerLocator` with
|
|
|
|
the `addIncomingInterceptor(Interceptor)` or
|
2014-12-04 10:25:29 -05:00
|
|
|
`addOutgoingInterceptor(Interceptor)` methods.
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
As noted above, if an interceptor returns `false` then the sending of the
|
|
|
|
packet is aborted which means that no other interceptors are be called and the
|
|
|
|
packet is not be processed further by the client. Typically this process
|
|
|
|
happens transparently to the client (i.e. it has no idea if a packet was
|
|
|
|
aborted or not). However, in the case of an outgoing packet that is sent in a
|
|
|
|
`blocking` fashion a `ActiveMQException` will be thrown to the caller. The
|
|
|
|
exception is thrown because blocking sends provide reliability and it is
|
|
|
|
considered an error for them not to succeed. `Blocking` sends occurs when, for
|
2014-12-04 10:25:29 -05:00
|
|
|
example, an application invokes `setBlockOnNonDurableSend(true)` or
|
2018-03-09 10:07:38 -05:00
|
|
|
`setBlockOnDurableSend(true)` on its `ServerLocator` or if an application is
|
|
|
|
using a JMS connection factory retrieved from JNDI that has either
|
|
|
|
`block-on-durable-send` or `block-on-non-durable-send` set to `true`. Blocking
|
|
|
|
is also used for packets dealing with transactions (e.g. commit, roll-back,
|
|
|
|
etc.). The `ActiveMQException` thrown will contain the name of the interceptor
|
|
|
|
that returned false.
|
|
|
|
|
|
|
|
As on the server, the client interceptor classes (and their dependencies) must
|
|
|
|
be added to the classpath to be properly instantiated and invoked.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
## Examples
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
See the following examples which show how to use interceptors:
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
- [Interceptor](examples.md#interceptor)
|
|
|
|
- [Interceptor AMQP](examples.md#interceptor-amqp)
|
|
|
|
- [Interceptor Client](examples.md#interceptor-client)
|
|
|
|
- [Interceptor MQTT](examples.md#interceptor-mqtt)
|