• Configuring MDP and Controlling it With and Without JMX


    This note demonstrates how to create and deploy an MDP, and how to control it, using both Spring’s inbuilt component management mechanisms, and JMX.

    Scenario

    We have a number of JMS listeners in our application. Having ported our hardware to a grid of virtual machines, we want the feature to switch-on and switch-off the listeners on the VMs, and re-allocate the listeners to VMs across the grid on-demand, to make best use of the resources.

    Scope

    This note just deals briefly with configuring the MDP, and switching it on and off.

    Concepts

    - What is an MDP?
    - MDP stands for Message Driven Pojo.

    In the EJB world we have MDBs (Message Driven Beans). These are standard JEE components which are configured on a server and which act as a Message Listener.

    Spring provides MDPs as an answer to MDBs.

    Some helpful links are:

    Creating a simple Spring MDP component, and controlling it using the Spring container provided “start” and “stop” methods

    MDP Spring configuration

    Extract from the Spring Config XML file spring_context_jms.xml

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <!-- Destination configuration -->
    <bean id="sample_queue"
            class="org.apache.activemq.command.ActiveMQQueue"
            autowire="constructor">
            <constructor-arg>
                <value>SAMPLE.QUEUE</value>
            </constructor-arg>
    </bean>
     
    <!-- Message Listener Container configuration -->
    <bean id="sample_jmsContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
            <property name="connectionFactory"
                ref="activeMqQueueConnectionFactory" />
            <property name="destination" ref=" sample_queue" />
            <property name="messageListener"
                ref="sample_messageListener" />
    </bean>
     
    <!-- Message Driven POJO (MDP) configuration -->
        <bean id="sample_messageListener"
            class="jmsexample.ExampleListener">
    <property name="listener" ref="sample_jmsContainer" />
        </bean>

    The sample class we create (sample_messageListener of class “jmsexample.ExampleListener”) must extend javax.jms.MessageListener and implement the “onMessage” method.

    As seen from the Spring configuration above, we have provided this as a ref to the “messageListener” property in the “DefaultMessageListenerContainer102” class.

    Also as seen above, we have provided a ref to “sample_jmsContainer” in the bean “sample_messageListener”. (This is so that we are able to expose it using JMX – as discussed further on).

    Configuring Spring components in a Web application in Tomcat

    The MDPs are deployed in a Web application as JMSWebApplication.war.
    Follows is an extract from the web.xml

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    <display-name>JMSWebApplication</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring_context.xml</param-value>
    </context-param>
    <listener>
        <description>Context Loader Listener</description>
        <listener-class>
    org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    Getting a handle to the Spring’s “Listener Container” component and calling start and stop on it (Non-JMX)

    Follows is a relevant extract from the client jsp:

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    <%@page import="org.springframework.beans.factory.BeanFactory,org.springframework.context.ApplicationContext,org.springframework.context.support.ClassPathXmlApplicationContext,org.springframework.jms.listener.DefaultMessageListenerContainer102,org.springframework.web.context.WebApplicationContext,org.springframework.web.context.support.WebApplicationContextUtils"%>
     
    ServletContext context = application
                    .getContext("/JMSWebApplication");
        WebApplicationContext wac = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(context);
     
    DefaultMessageListenerContainer102 listener = (DefaultMessageListenerContainer102) wac
                    .getBean("sample_jmsContainer");
     
    listener.stop();
    //listener.start();

    Exposing and Controlling the Listener component using Spring JMX

    This involves configuring Spring JMX.
    Since this is deployed in Tomcat, we will enable JMX in Tomcat and connect to it using jconsole to control this component.

    Configuring Spring JMX (server/container), and Exposing the component in JMX

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <bean id="exporter"
            class="org.springframework.jmx.export.MBeanExporter">
            <property name="beans">
                <map>
                    <entry key="bean:name=sample_listener_jmx"
                        value-ref=" sample_messageListener" />
                </map>
            </property>
        </bean>

    Arguments to Tomcat to enable JMX

    Provide these (self-explanatory) arguments to the JVM while starting up Tomcat

    1
    2
    3
    -Dcom.sun.management.jmxremote.port=10088 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false \

    Code skeleton of jmsexample. ExampleListener

    (Note the “startListener” and “stopListener” methods, and use of the “DefaultMessageListenerContainer102” class.)

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    package jmsexample;
    < imports come here ... >
     
    public class ExampleListener implements MessageListener {
     
        private DefaultMessageListenerContainer102 listener;
        < other variables here ... >
     
        public void onMessage(Message message) {
     
            < Business Logic here ... >
     
        }
     
        @ManagedAttribute(description = "Starts the Listener", currencyTimeLimit = 15)
        public void startListener() {
     
            System.out.println("Starting listener");
            getListener().start();
            System.out.println("listener started " + getListener().isRunning());
        }
     
        @ManagedAttribute(description = "Stops the Listener", currencyTimeLimit = 15)
        public void stopListener() {
     
            System.out.println("Stopping listener");
            getListener().stop();
            System.out.println("listener stopped " + getListener().isRunning());
        }
     
        public DefaultMessageListenerContainer102 getListener() {
            return listener;
        }
     
        public void setListener(DefaultMessageListenerContainer102 listener) {
            this.listener = listener;
        }
     
        < other methods here ...>
    }

    View in jconsole of controlling the component

    (Note the “startListener” and “stopListener” methods exposed)
    The remote connection string here, for example, is service:jmx:rmi:///jndi/rmi://localhost:10088/jmxrmi
    jconsole-controller

    A simple java based rmi client using Tomcat JMX to control the components

    Client code snippet and configuration

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    String address =
             "service:jmx:rmi:///jndi/rmi://localhost:10088/jmxrmi";
        JMXServiceURL serviceURL = new JMXServiceURL(address);
        Map<String, Object> environment = null;
        JMXConnector connector = JMXConnectorFactory.connect(serviceURL,
                    environment);
        MBeanServerConnection mBeanConnection = connector
                    .getMBeanServerConnection();
     
        ObjectName exampleServiceName = ObjectName
                    .getInstance("bean:name=sample_listener_jmx");
     
    mBeanConnection.invoke(exampleServiceName, "startListener", null, null);
    //mBeanConnection.invoke(exampleServiceName, "stopListener", null, null);
  • 相关阅读:
    docker与虚拟机性能比较
    CAP原则(CAP定理)、BASE理论
    CAP 定理的含义
    JVM监测分析JConsole
    JConsole详解
    jconsole工具使用
    轻松看懂Java字节码
    JVM 虚拟机字节码指令表
    深入理解java虚拟机(六)字节码指令简介
    大话+图说:Java字节码指令——只为让你懂
  • 原文地址:https://www.cnblogs.com/chenying99/p/2470163.html
Copyright © 2020-2023  润新知