72 lines
4.0 KiB
XML
72 lines
4.0 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
||
<!-- ============================================================================= -->
|
||
<!-- Copyright © 2009 Red Hat, Inc. and others. -->
|
||
<!-- -->
|
||
<!-- The text of and illustrations in this document are licensed by Red Hat under -->
|
||
<!-- a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). -->
|
||
<!-- -->
|
||
<!-- An explanation of CC-BY-SA is available at -->
|
||
<!-- -->
|
||
<!-- http://creativecommons.org/licenses/by-sa/3.0/. -->
|
||
<!-- -->
|
||
<!-- In accordance with CC-BY-SA, if you distribute this document or an adaptation -->
|
||
<!-- of it, you must provide the URL for the original version. -->
|
||
<!-- -->
|
||
<!-- Red Hat, as the licensor of this document, waives the right to enforce, -->
|
||
<!-- and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent -->
|
||
<!-- permitted by applicable law. -->
|
||
<!-- ============================================================================= -->
|
||
|
||
<chapter id="intercepting-operations">
|
||
<title>拦截操作</title>
|
||
<para>ActiveMQ支持<emphasis>拦截器</emphasis>。拦截器可以拦截进入服务器的数据包。每进入服务器
|
||
一个数据包,拦截器就被调用一次,允许一些特殊和处理,例如对包的审计、过滤等。拦截器可以对数据包
|
||
进行改动。</para>
|
||
<section>
|
||
<title>实现拦截器</title>
|
||
<para>拦截器必须要实现<literal>Interceptor接口</literal>:</para>
|
||
<programlisting>
|
||
package org.apache.activemq.api.core.interceptor;
|
||
|
||
public interface Interceptor
|
||
{
|
||
boolean intercept(Packet packet, RemotingConnection connection)
|
||
throws ActiveMQException;
|
||
}
|
||
</programlisting>
|
||
<para>它的方法的返回值是很重要的:</para>
|
||
<itemizedlist>
|
||
<listitem>
|
||
<para>如果返回<literal>true</literal>,处理正常进行下去。</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>如果返回<literal>false</literal>,则处理被中断,其它的拦截器将不会被调用,数据包将不会
|
||
被服务器所处理。</para>
|
||
</listitem>
|
||
</itemizedlist>
|
||
</section>
|
||
<section>
|
||
<title>配置拦截器</title>
|
||
<para>拦截器的配置在<literal>activemq-configuration.xml</literal>文件中:</para>
|
||
<programlisting>
|
||
<remoting-interceptors>
|
||
<class-name>org.apache.activemq.jms.example.LoginInterceptor</class-name>
|
||
<class-name>org.apache.activemq.jms.example.AdditionalPropertyInterceptor</class-name>
|
||
</remoting-interceptors>
|
||
</programlisting>
|
||
<para>拦截器的类(以及它们依赖的类)必须要在服务器的classpath中,否则不能被正常初始化及调用。</para>
|
||
</section>
|
||
<section>
|
||
<title>客户端拦截器</title>
|
||
<para>在客户端也可以有拦截器来拦截<emphasis>来自服务器</emphasis>的数据包。<code>ClientSessionFactory</code>
|
||
的<code>addInterceptor()</code>方法可以用来添加拦截器。</para>
|
||
<para>同样拦截器的类(以及它们依赖的类)必须要在客户端的classpath中,否则它们不能被正常初始化及调用。</para>
|
||
</section>
|
||
<section>
|
||
<title>例子</title>
|
||
<para>参见<xref linkend="examples.interceptor" />。这个例子中展示了如何使用拦截器向发往服务器的消息中
|
||
添加属性。</para>
|
||
</section>
|
||
</chapter>
|