自由激活@MessageDriven

Liberty activate @MessageDriven

我想在 liberty 应用程序服务器上激活自定义消息侦听器。

这是我的代码:

@MessageDriven(name = "Receiver")
public class Receiver implements InboundListener {

    @Override
    public void receiveMessage(String message) {
        System.out.println("Message Received : " + message);
    }
}      

这是 server.xml :

<?xml version="1.0" encoding="UTF-8"?>
<server description="Dandelion IOT server">
    <featureManager>
        <feature>cdi-2.0</feature>
        <feature>beanValidation-2.0</feature>
        <feature>appSecurity-3.0</feature>
        <feature>jpa-2.2</feature>
        <feature>jaxrs-2.1</feature>
        <feature>jsonb-1.0</feature>
        <feature>jsonp-1.1</feature>
        <feature>managedBeans-1.0</feature>
        <feature>websocket-1.1</feature>
        <feature>ejbLite-3.2</feature>
        <feature>jca-1.7</feature>
        <feature>jndi-1.0</feature>
        <feature>mdb-3.2</feature>
        <feature>localConnector-1.0</feature>
    </featureManager>
    <library id="DandelionLibs">
        <fileset dir="/etc/dandelion/lib" includes="*.jar"/>
    </library>
    <jdbcDriver id="database-driver" libraryRef="DandelionLibs"/>
    <dataSource jndiName="JTA-DataSource" transactional="true" jdbcDriverRef="database-driver">
        <properties databaseName="${database.name}" serverName="${database.hostname}" portNumber="${database.port}"
                    user="${database.username}" password="${database.password}"/>
    </dataSource>

    <resourceAdapter id="dra" autoStart="true" location="/etc/dandelion/lib/RA.rar"/>

    <connectionFactory jndiName="h5/sampleConnection">
        <properties.dra/>
    </connectionFactory>

    <activationSpec id="h5/inboundListener">
        <properties.dra.DandelionActivationSpec/>
    </activationSpec>

    <webApplication id="dandelion-web"
                    location="dandelion-war-0.1-SNAPSHOT.war"
                    name="dandelion-web">
        <classloader classProviderRef="dra"/>
    </webApplication>

    <basicRegistry id="basic" realm="BasicRealm"/>
    <httpSession securityIntegrationEnabled="false"/>
    <httpEndpoint id="defaultHttpEndpoint" httpPort="8080" httpsPort="9443">
        <httpOptions http2="enabled"/>
    </httpEndpoint>
    <webContainer disableXPoweredBy="true"/>
    <applicationManager autoExpand="true"/>
    <applicationMonitor updateTrigger="mbean"/>
</server>    

和我的激活规范:

@Activation(messageListeners = InboundListener.class)
public class DandelionActivationSpec implements ActivationSpec {

    private ResourceAdapter resourceAdapter;

    @Override
    public void validate() throws InvalidPropertyException {
    }

    @Override
    public ResourceAdapter getResourceAdapter() {
        return resourceAdapter;
    }

    @Override
    public void setResourceAdapter(ResourceAdapter ra) throws ResourceException {
        this.resourceAdapter = ra;
    }
}       

和ra.xml:

<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
           http://xmlns.jcp.org/xml/ns/javaee/connector_1_7.xsd"
           version="1.7">
    <description>Sample Resource Adapter</description>
    <display-name>Sample Resource Adapter</display-name>
    <eis-type>Sample Resource Adapter</eis-type>
    <resourceadapter-version>1.0</resourceadapter-version>
    <license>
        <license-required>false</license-required>
    </license>
    <resourceadapter>
        <resourceadapter-class>org.company.dandelion.adapter.DandelionResourceAdapter</resourceadapter-class>
        <outbound-resourceadapter>
            <connection-definition>
                <managedconnectionfactory-class>org.company.dandelion.adapter.DandelionManagedConnectionFactory</managedconnectionfactory-class>
                <connectionfactory-interface>org.company.dandelion.api.DandelionConnectionFactory</connectionfactory-interface>
                <connectionfactory-impl-class>org.company.dandelion.adapter.DandelionConnectionFactoryImpl</connectionfactory-impl-class>
                <connection-interface>org.company.dandelion.api.DandelionConnection</connection-interface>
                <connection-impl-class>org.company.dandelion.adapter.DandelionConnectionImpl</connection-impl-class>
            </connection-definition>
            <transaction-support>NoTransaction</transaction-support>
            <reauthentication-support>false</reauthentication-support>
        </outbound-resourceadapter>
        <inbound-resourceadapter>
            <messageadapter>
                <messagelistener>
                    <messagelistener-type>org.company.dandelion.api.InboundListener</messagelistener-type>
                    <activationspec>
                        <activationspec-class>org.company.dandelion.adapter.DandelionActivationSpec</activationspec-class>
                    </activationspec>
                </messagelistener>
            </messageadapter>
        </inbound-resourceadapter>
    </resourceadapter>
</connector>

Liberty 应用程序服务器结果在控制台上登录:

[WARNING ] CNTR4015W: The message endpoint for the Receiver message-driven bean cannot be activated because the dandelion-war-0.1-SNAPSHOT/Receiver activation specification is not available. The message endpoint will not receive messages until the activation specification becomes available.        

出站资源适配器工作没有任何问题,但入站没有收到任何消息!
我在问这个问题之前阅读了这个链接,但不明白如何解决这个问题:
Link 1

Link 3
configuration/Code 的哪一部分有问题?

更新:
我将配置更改为:

<activationSpec id="dandelion-web/Receiver">
    <properties.dra/>
</activationSpec>
<webApplication id="dandelion-web"
                location="dandelion-web.war"
                name="dandelion-web">
    <classloader classProviderRef="dra"/>
</webApplication>     

现在在控制台上收到此错误:

[AUDIT   ] J2CA7001I: Resource adapter dra installed in 1.911 seconds.
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:8080/
[AUDIT   ] CWWKZ0001I: Application dandelion started in 8.064 seconds.
[err] javax.resource.spi.UnavailableException: activating thread not allowed to create endpoint during activation.
[err]   at com.ibm.ws.ejbcontainer.mdb.BaseMessageEndpointFactory.createEndpoint(BaseMessageEndpointFactory.java:406)
[err]   at [internal classes]
[err]   at com.company.dandelion.adapter.DandelionResourceAdapter.endpointActivation(DandelionResourceAdapter.java:56)
[err]   at com.ibm.ws.jca.service.EndpointActivationService.activateEndpoint(EndpointActivationService.java:585)
[err]   at [internal classes]
[err]   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[err]   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[err]   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[err]   at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[err]   at org.apache.felix.scr.impl.inject.methods.BaseMethod.invokeMethod(BaseMethod.java:242)
[err]   at org.apache.felix.scr.impl.inject.methods.BaseMethod.access0(BaseMethod.java:41)
[err]   at org.apache.felix.scr.impl.inject.methods.BaseMethod$Resolved.invoke(BaseMethod.java:678)
[err]   at [internal classes]
[err]   at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[err]   at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[err]   at java.base/java.lang.Thread.run(Thread.java:834)
[AUDIT   ] CWWKF0012I: The server installed the following features: [appSecurity-3.0, beanValidation-2.0, cdi-2.0, distributedMap-1.0, ejbLite-3.2, el-3.0, jaxrs-2.1, jaxrsClient-2.1, jca-1.7, jdbc-4.2, jndi-1.0, jpa-2.2, jpaContainer-2.2, jsonb-1.0, jsonp-1.1, localConnector-1.0, managedBeans-1.0, mdb-3.2, servlet-4.0, ssl-1.0, websocket-1.1].
[AUDIT   ] CWWKF0011I: The dandelion server is ready to run a smarter planet. The dandelion server started in 12.919 seconds.     

这是我的蒲公英资源适配器:

@Connector(
        description = "Sample Resource Adapter",
        displayName = "Sample Resource Adapter",
        eisType = "Sample Resource Adapter",
        version = "1.0"
)
public class DandelionResourceAdapter implements ResourceAdapter {

    final Map<DandelionActivationSpec, EndpointTarget> targets = new ConcurrentHashMap<>();

    public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException {
    }

    public void stop() {
    }

    public void endpointActivation(final MessageEndpointFactory messageEndpointFactory,
                                   final ActivationSpec activationSpec) {
        final DandelionActivationSpec sampleActivationSpec = (DandelionActivationSpec) activationSpec;

        // ->>>>>>>>> EXCEPTION FOR THIS LINES : 
        try {
            final MessageEndpoint messageEndpoint = messageEndpointFactory.createEndpoint(null);
            final EndpointTarget target = new EndpointTarget(messageEndpoint);
            targets.put(sampleActivationSpec, target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endpointDeactivation(MessageEndpointFactory messageEndpointFactory, ActivationSpec activationSpec) {
        final DandelionActivationSpec sampleActivationSpec = (DandelionActivationSpec) activationSpec;

        final EndpointTarget endpointTarget = targets.get(sampleActivationSpec);
        if (endpointTarget == null) {
            throw new IllegalStateException("No EndpointTarget to undeploy for ActivationSpec " + activationSpec);
        }

        endpointTarget.messageEndpoint.release();
    }

    public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException {
        return new XAResource[0];
    }

    public void sendMessage(final String message) {
        final Collection<EndpointTarget> endpoints = this.targets.values();
        for (final EndpointTarget endpoint : endpoints) {
            endpoint.invoke(message);
        }
    }

    public static class EndpointTarget {
        private final MessageEndpoint messageEndpoint;

        public EndpointTarget(final MessageEndpoint messageEndpoint) {
            this.messageEndpoint = messageEndpoint;
        }

        public void invoke(final String message) {
            ((InboundListener) this.messageEndpoint).receiveMessage(message);
        }
    }
}

伊朗人有句谚语说:寻找者就是发现者
终于在互联网上搜索了大约 10 天并编写了简单的代码。我找到了如何解决这个问题。
取决于应用程序服务器,不应在 endpointActivation 方法上创建消息端点。
实际上 TomEE 应用程序服务器不会对此代码产生任何异常,但自由应用程序服务器会导致此异常。
我写了几个简单的应用程序并发布在 github.
您可以在 TomEE 或 Liberty 上编译和 运行 此应用程序。
以后我会发布更多的例子,大家也可以使用。

My GitHub Repository

我也收到了 CNTR4015W: 无法激活 .. bean 的消息端点,因为 .. 激活规范不可用。

问题是,我用过.ear-name/.mdb-name/ejb-name 但也在 ejb-jar.xml 中设置了不同的 <module-name> (具有优先权)

这在注释中记录在 https://www.ibm.com/support/knowledgecenter/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_dep_msg_mdbjca.html