无法实例化 class: org.apache.activemq.jndi.ActiveMQInitialContextFactory,使用 jmeter-maven-plugin

Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory, using jmeter-maven-plugin

简而言之,如果我 运行 下面来自 Maven 的 JMeter 计划测试,它会给出结果:

javax.naming.NamingException: javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.activemq.jndi.ActiveMQInitialContextFactory]

然而,如果我 运行 在没有 maven 插件的情况下测试相同的 JMeter 计划,它 运行 是正确的。

提供更多详细信息:

如果我以这种方式启动 ActiveMQ(Windows 命令提示符):

cd C:\_d\server\ActiveMQ\apache-activemq-5.9.0\bin
activemq.bat

然后,我 运行 完全相同的 jmx 脚本:

jmeter.bat -n -t C:\_d\scripts\JMeter\JMS_SQS_MVN\SQS.jmx -l C:\temp\activemq.jtl -j C:\temp\activemq.jmx.log

我可以转到 ActiveMq 控制台并查看创建的标记为 sqs_dummy_mvn6 的队列,因为它出现在此类 jmx 脚本中。

现在,通过 maven 插件尝试相同的脚本,它只显示 "Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory"。好吧,我知道,每当我使用 jmeter-maven-plugin 时,嵌入式 jmeter 和嵌入式 activemq 都会在验证阶段启动和停止。

我正在通过 Maven 调用相同的脚本:

mvn clean verify

...\artproducer\src\test\jmeter\SQS.jmx(下面只启用发布者;禁用订阅以使测试更简单)

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.9" jmeter="3.0 r1743807">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Plano de Teste SQS" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="Variáveis Definidas Pelo Usuário" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Grupo de Usuários - Consumers" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Controlador de Iteração" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">1</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <longProp name="ThreadGroup.start_time">1484067293000</longProp>
        <longProp name="ThreadGroup.end_time">1484067293000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <SubscriberSampler guiclass="JMSSubscriberGui" testclass="SubscriberSampler" testname="JMS Subscriber" enabled="false">
          <stringProp name="jms.jndi_properties">false</stringProp>
          <stringProp name="jms.initial_context_factory">org.apache.activemq.jndi.ActiveMQInitialContextFactory</stringProp>
          <stringProp name="jms.provider_url">tcp://localhost:61616</stringProp>
          <stringProp name="jms.connection_factory">ConnectionFactory</stringProp>
          <stringProp name="jms.topic">dynamicQueues/sqs_dummy_mvn6</stringProp>
          <stringProp name="jms.security_principle"></stringProp>
          <stringProp name="jms.security_credentials"></stringProp>
          <boolProp name="jms.authenticate">false</boolProp>
          <stringProp name="jms.iterations">1</stringProp>
          <stringProp name="jms.read_response">true</stringProp>
          <stringProp name="jms.client_choice">jms_subscriber_on_message</stringProp>
          <boolProp name="jms.stop_between_samples">true</boolProp>
          <stringProp name="jms.timeout">60000</stringProp>
          <boolProp name="jms.destination_static">false</boolProp>
        </SubscriberSampler>
        <hashTree>
          <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="false">
            <boolProp name="ResultCollector.error_logging">false</boolProp>
            <objProp>
              <name>saveConfig</name>
              <value class="SampleSaveConfiguration">
                <time>true</time>
                <latency>true</latency>
                <timestamp>true</timestamp>
                <success>true</success>
                <label>true</label>
                <code>true</code>
                <message>true</message>
                <threadName>true</threadName>
                <dataType>true</dataType>
                <encoding>false</encoding>
                <assertions>true</assertions>
                <subresults>true</subresults>
                <responseData>false</responseData>
                <samplerData>false</samplerData>
                <xml>false</xml>
                <fieldNames>true</fieldNames>
                <responseHeaders>false</responseHeaders>
                <requestHeaders>false</requestHeaders>
                <responseDataOnError>false</responseDataOnError>
                <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
                <assertionsResultsToSave>0</assertionsResultsToSave>
                <bytes>true</bytes>
                <threadCounts>true</threadCounts>
                <idleTime>true</idleTime>
              </value>
            </objProp>
            <stringProp name="filename"></stringProp>
          </ResultCollector>
          <hashTree/>
        </hashTree>
        <ResultCollector guiclass="GraphVisualizer" testclass="ResultCollector" testname="Graph Results" enabled="false">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="false">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
        <PublisherSampler guiclass="JMSPublisherGui" testclass="PublisherSampler" testname="JMS Publisher" enabled="true">
          <stringProp name="jms.jndi_properties">false</stringProp>
          <stringProp name="jms.initial_context_factory">org.apache.activemq.jndi.ActiveMQInitialContextFactory</stringProp>
          <stringProp name="jms.provider_url">tcp://localhost:61616</stringProp>
          <stringProp name="jms.connection_factory">ConnectionFactory</stringProp>
          <stringProp name="jms.topic">dynamicQueues/sqs_dummy_mvn6</stringProp>
          <stringProp name="jms.expiration"></stringProp>
          <stringProp name="jms.priority"></stringProp>
          <stringProp name="jms.security_principle"></stringProp>
          <stringProp name="jms.security_credentials"></stringProp>
          <stringProp name="jms.text_message">testando a partir do JMeter4444</stringProp>
          <stringProp name="jms.input_file"></stringProp>
          <stringProp name="jms.random_path"></stringProp>
          <stringProp name="jms.config_choice">jms_use_text</stringProp>
          <stringProp name="jms.config_msg_type">jms_text_message</stringProp>
          <stringProp name="jms.iterations">1</stringProp>
          <boolProp name="jms.authenticate">false</boolProp>
          <elementProp name="jms.jmsProperties" elementType="JMSProperties">
            <collectionProp name="JMSProperties.properties"/>
          </elementProp>
          <boolProp name="jms.destination_static">false</boolProp>
        </PublisherSampler>
        <hashTree>
          <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="false">
            <boolProp name="ResultCollector.error_logging">false</boolProp>
            <objProp>
              <name>saveConfig</name>
              <value class="SampleSaveConfiguration">
                <time>true</time>
                <latency>true</latency>
                <timestamp>true</timestamp>
                <success>true</success>
                <label>true</label>
                <code>true</code>
                <message>true</message>
                <threadName>true</threadName>
                <dataType>true</dataType>
                <encoding>false</encoding>
                <assertions>true</assertions>
                <subresults>true</subresults>
                <responseData>false</responseData>
                <samplerData>false</samplerData>
                <xml>false</xml>
                <fieldNames>true</fieldNames>
                <responseHeaders>false</responseHeaders>
                <requestHeaders>false</requestHeaders>
                <responseDataOnError>false</responseDataOnError>
                <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
                <assertionsResultsToSave>0</assertionsResultsToSave>
                <bytes>true</bytes>
                <threadCounts>true</threadCounts>
                <idleTime>true</idleTime>
              </value>
            </objProp>
            <stringProp name="filename"></stringProp>
          </ResultCollector>
          <hashTree/>
        </hashTree>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

...\artproducer\target\jmeter\results170111-SQS.jtl

<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<sample t="0" it="0" lt="0" ts="0" s="false" lb="JMS Publisher" rc="000" rm="javax.naming.NamingException: javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.activemq.jndi.ActiveMQInitialContextFactory]" tn="Grupo de Usuários - Consumers 1-1" dt="" by="0" ng="1" na="1"/>

</testResults>

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>grpactivemq</groupId>
    <artifactId>artproducer</artifactId>
    <!-- <packaging>pom</packaging> -->
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>artproducer Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <!-- enviando queue para o broker do ActiveMq iniciado como serviço -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
        <activemq.version>5.9.0</activemq.version>
        <spring.version>4.1.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- activemq -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>${activemq.version}</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- xbean -->
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>3.7</version>
        </dependency>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <!-- <scope>test</scope> -->
        </dependency>
    </dependencies>

    <build>
        <plugins>

<!--            <plugin>
                <artifactId>exec-maven-plugin</artifactId>
                <groupId>org.codehaus.mojo</groupId>
                <executions>
                    <execution>
                        <id>Run load Test</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${basedir}/scripts/jmeter_exe.sh</executable>
                        </configuration>
                    </execution>
                </executions>
            </plugin> -->

            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>2.0.3</version>
                <executions>
                    <execution>
                        <id>jmeter-test</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.activemq</groupId>
                        <artifactId>activemq-all</artifactId>
                        <version>${activemq.version}</version>
                    </dependency>
                </dependencies>  
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>

        <finalName>artproducer</finalName>

    </build>
</project>

我认为您需要将 activemq-all.jar 依赖项添加到 JMeter 的 /lib/ext 文件夹,如 here 所述,这与您使用的 <dependency> 语法略有不同:

        <plugin>
            <groupId>com.lazerycode.jmeter</groupId>
            <artifactId>jmeter-maven-plugin</artifactId>
            <version>2.0.3</version>
            <executions>
                <execution>
                    <id>jmeter-test</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jmeter</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jmeterExtensions>
                    <artifact>org.apache.activemq:activemq-all:jar:${activemq.version}</artifact>
                </jmeterExtensions>
            </configuration>
        </plugin>

使用 plugin

的 2.6.0 版本

并在配置节点下添加:

 <testPlanLibraries>
    <artifact>org.apache.activemq:activemq-spring:5.15.2</artifact>
    <artifact>org.apache.activemq:activemq-client:5.15.2</artifact>
    <artifact>org.apache.activemq:activemq-broker:5.15.2</artifact>
</testPlanLibraries>
<!-- Remove broken JARS -->
<excludedArtifacts>
    <exclusion>commons-pool2:commons-pool2</exclusion>
    <exclusion>commons-math3:commons-math3</exclusion>
    <exclusion>com.sun.jdmk:jmxtools</exclusion>
    <exclusion>com.sun.jmx:jmxri</exclusion>
</excludedArtifacts>