검색

11장. 메시지 가로채기

download PDF

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

비즈니스 요구 사항에 맞게 인터셉터를 개발할 수 있습니다. 인터셉터는 프로토콜마다 다르며 적절한 인터페이스를 구현해야 합니다.

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

11.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;
        }
      }
    • 코어 프로토콜을 사용하는 경우 인터셉터는 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.StompInterceptor 인터페이스를 구현합니다.

      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;
        }
      }
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.