我希望激活自由应用服务器上的自定义消息侦听器。
这是我的密码:
@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>自由应用服务器将此日志记录在控制台上:
[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. 出站资源适配器工作没有任何问题,但入站没有收到任何消息!
我在问这个问题之前看过这个链接,但不明白如何解决这个问题:
配置/代码的哪个部分有问题?
更新:
我将配置更改为:
<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.access$500(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. 这是我的DandelionResourceAdapter:
@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);
}
}
}发布于 2020-07-24 16:09:13
伊朗人有一句谚语说:探索者是寻找者,
最后,在大约10天之后,在网上搜索并编写了简单的代码。我找到了解决这个问题的方法。
依赖于应用服务器,不应该在endpointActivation方法上创建Message。
实际上,TomEE应用服务器不会在此代码上产生任何异常,但是自由应用程序服务器会导致此异常。
我很少编写简单的应用程序并在github上发布。
您可以在TomEE或TomEE上编译和运行此应用程序。
我将在将来发表更多的例子,所以你也可以使用它。
发布于 2020-08-28 08:29:15
我还得到了CNTR4015W:.不能激活bean,因为..。激活规范不可用。
问题是,我使用了..ear name/..mdb name/ejb-name,但也在ejb-jar.xml (具有优先级)中设置了不同的<module-name>。
这在mdbjca.html的说明中有记录。
https://stackoverflow.com/questions/62716169
复制相似问题