activemq-artemis/docs/user-manual/intercepting-operations.adoc

95 lines
4.3 KiB
Plaintext
Raw Normal View History

ARTEMIS-4383 migrate user docs to AsciiDoc Markdown, which is currently used for user-facing documentation, is good for a lot of things. However, it's not great for the kind of complex documentation we have and our need to produce both multi-page HTML and single-page PDF output via Maven. Markdown lacks features which would make the documentation easier to read, easier to navigate, and just look better overall. The current tool-chain uses honkit and a tool called Calibre. Honkit is written in TypeScript and is installed via NPM. Calibre is a native tool so it must be installed via an OS-specific package manager. All this complexity makes building, releasing, uploading, etc. a pain. AsciiDoc is relatively simple like Markdown, but it has more features for presentation and navigation not to mention Java-based Maven tooling to generate both HTML and PDF. Migrating will improve both the appearance of the documentation as well as the processes to generate and upload it. This commit contains the following changes: - Convert all the Markdown for the User Manual, Migration Guide, and Hacking guide to AsciiDoc via kramdown [1]. - Update the `artemis-website` build to use AsciiDoctor Maven tooling. - Update `RELEASING.md` with simplified instructions. - Update Hacking Guide with simplified instructions. - Use AsciiDoc link syntax in Artemis Maven doc plugin. - Drop EPUB & MOBI docs for User Manual as well as PDF for the Hacking Guide. All docs will be HTML only except for the User Manual which will have PDF. - Move all docs up out of their respective "en" directory. This was a hold-over from when we had docs in different languages. - Migration & Hacking Guides are now single-page HTML since they are relatively short. - Refactor README.md to simplify and remove redundant content. Benefits of the change: - Much simplified tooling. No more NPM packages or native tools. - Auto-generated table of contents for every chapter. - Auto-generated anchor links for every sub-section. - Overall more appealing presentation. - All docs will use the ActiveMQ favicon. - No more manual line-wrapping! AsciiDoc recommends one sentence per line and paragraphs are separated by a blank line. - AsciiDoctor plugins for IDEA are quite good. - Resulting HTML is less than *half* of the previous size. All previous links/bookmarks should continue to work. [1] https://github.com/asciidoctor/kramdown-asciidoc
2023-07-27 23:45:17 -04:00
= Intercepting Operations
:idprefix:
:idseparator: -
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.
== Implementing The Interceptors
All interceptors are protocol specific.
An interceptor for the core protocol must implement the interface `Interceptor`:
[,java]
----
package org.apache.activemq.artemis.api.core.interceptor;
public interface Interceptor {
boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException;
}
----
For stomp protocol an interceptor must implement the interface `StompFrameInterceptor`:
[,java]
----
package org.apache.activemq.artemis.core.protocol.stomp;
public interface StompFrameInterceptor extends BaseInterceptor<StompFrame> {
boolean intercept(StompFrame stompFrame, RemotingConnection connection);
}
----
Likewise for MQTT protocol, an interceptor must implement the interface `MQTTInterceptor`:
[,java]
----
package org.apache.activemq.artemis.core.protocol.mqtt;
public interface MQTTInterceptor extends BaseInterceptor<MqttMessage> {
boolean intercept(MqttMessage mqttMessage, RemotingConnection connection);
}
----
The returned boolean value is important:
* if `true` is returned, the process continues normally
* 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.
== Configuring The Interceptors
Both incoming and outgoing interceptors are configured in `broker.xml`:
[,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>
<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>
----
See the documentation on xref:using-server.adoc#adding-runtime-dependencies[adding runtime dependencies] to understand how to make your interceptor available to the broker.
== Interceptors on the Client Side
The interceptors can also be run on the Apache ActiveMQ Artemis 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 `addOutgoingInterceptor(Interceptor)` methods.
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 example, an application invokes `setBlockOnNonDurableSend(true)` or `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.
== Examples
See the following examples which show how to use interceptors:
* xref:examples.adoc#interceptor[Interceptor]
* xref:examples.adoc#interceptor-amqp[Interceptor AMQP]
* xref:examples.adoc#interceptor-client[Interceptor Client]
* xref:examples.adoc#interceptor-mqtt[Interceptor MQTT]