13장. 메시지 가로채기

AMQ Broker를 사용하면 브로커 입력 또는 종료 패킷을 가로채서 패킷을 감사하거나 메시지를 필터링할 수 있습니다. 인터셉터는 가로채는 패킷을 변경할 수 있으므로 이를 통해 강력하면서도 잠재적으로 위험할 수 있습니다.

비즈니스 요구 사항을 충족하기 위해 인터셉터를 개발할 수 있습니다. 인터셉터는 프로토콜별로 고유하며 적절한 인터페이스를 구현해야 합니다.

인터셉터는 부울 값을 반환하는 intercept() 메서드를 구현해야 합니다. 값이 true 이면 메시지 패킷이 계속 수행됩니다. false 인 경우 프로세스가 중단되고 다른 인터셉터는 호출되지 않으며 메시지 패킷은 더 이상 처리되지 않습니다.

13.1. 인터셉터 생성

들어오고 나가는 자체 인터셉터를 생성할 수 있습니다. 모든 인터셉터는 프로토콜별로 고유하며 서버를 각각 입력하거나 종료하는 모든 패킷에 대해 호출됩니다. 이를 통해 인터셉터를 생성하여 감사 패킷과 같은 비즈니스 요구 사항을 충족할 수 있습니다. 인터셉터는 가로채는 패킷을 변경할 수 있습니다. 이렇게하면 강력하며 잠재적으로 위험 할 수 있으므로 신중하게 사용하십시오.

인터셉터 및 해당 종속 항목은 브로커의 Java 클래스 경로에 배치되어야 합니다. 기본적으로 classpath의 일부이므로 BROKER_INSTANCE_DIR/lib 디렉토리를 사용할 수 있습니다.

절차

다음 예제에서는 전달된 각 패킷의 크기를 확인하는 인터셉터를 생성하는 방법을 보여줍니다. 이 예제에서는 각 프로토콜에 대해 특정 인터페이스를 구현합니다.

  • 적절한 인터페이스를 구현하고 해당 intercept() 메서드를 재정의합니다.

    • AMQP 프로토콜을 사용하는 경우 org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage;
      import org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements AmqpInterceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        public boolean intercept(final AMQPMessage message, RemotingConnection connection)
        {
          int size = message.getEncodeSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This AMQPMessage has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    • Core Protocol을 사용하는 경우 인터셉터는 org.apache.artemis.activemq.api.core.Interceptor 인터페이스를 구현해야 합니다.

      package com.example;
      
      import org.apache.artemis.activemq.api.core.Interceptor;
      import org.apache.activemq.artemis.core.protocol.core.Packet;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(Packet packet, RemotingConnection connection)
        throws ActiveMQException
        {
          int size = packet.getPacketSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This Packet has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    • MQTT 프로토콜을 사용하는 경우 org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor;
      import io.netty.handler.codec.mqtt.MqttMessage;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(MqttMessage mqttMessage, RemotingConnection connection)
        throws ActiveMQException
        {
          byte[] msg = (mqttMessage.toString()).getBytes();
          int size = msg.length;
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This MqttMessage has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    • STOMP 프로토콜을 사용하는 경우 org.apache.activemq.artemis.core.protocol.stomp.StompFrameInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.core.protocol.stomp.StompFrameInterceptor;
      import org.apache.activemq.artemis.core.protocol.stomp.StompFrame;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(StompFrame stompFrame, RemotingConnection connection)
        throws ActiveMQException
        {
          int size = stompFrame.getEncodedSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This StompFrame has an acceptable size.");
            return true;
          }
          return false;
        }
      }