How to disable ResourceAdapter annotation in Wildfly

78 views Asked by At

I have a java code that contains the @ResourceAdapter annotation in my EAR that I deploy in Wildfly:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "useJndi", propertyValue = "true"),
    @ActivationConfigProperty(propertyName = "destination",
        propertyValue = "java:/jms/queue/myqueue"),
})
@ResourceAdapter("my-resource-adapter")
public class MyMessageListener implements MessageListener {

  @Override
  public void onMessage (Message message) {
  }
}

I add the resource adapter via CLI:

/subsystem=resource-adapters/resource-adapter=my-resource-adapter:add(archive=activemq-rar-5.18.2.rar,transaction-support=XATransaction)
/subsystem=resource-adapters/resource-adapter=my-resource-adapter/config-properties=ServerUrl:add(value=<url>)
/subsystem=resource-adapters/resource-adapter=my-resource-adapter/connection-definitions=my-resource-adapter.connection:add(<connection definitions here>)
/subsystem=resource-adapters/resource-adapter=my-resource-adapter/admin-objects=<admin objects here>
/subsystem=resource-adapters/resource-adapter=my-resource-adapter:activate

Everything works fine. Now, I want to introduce a boolean property whether to use and deploy the ActiveMQ resource adapter or not. Initially, I did this by having an if statement:

set use_resource_adapter=${use_resource_adapter}

if (outcome == "success" && result == "true") of :resolve-expression(expression=$use_resource_adapter)
  /subsystem=resource-adapters/resource-adapter=my-resource-adapter:add(archive=activemq-rar-5.18.2.rar,transaction-support=XATransaction)
  /subsystem=resource-adapters/resource-adapter=my-resource-adapter/config-properties=ServerUrl:add(value=<url>)
  /subsystem=resource-adapters/resource-adapter=my-resource-adapter/connection-definitions=my-resource-adapter.connection:add(<connection definitions here>)
  /subsystem=resource-adapters/resource-adapter=my-resource-adapter/admin-objects=<admin objects here>
  /subsystem=resource-adapters/resource-adapter=my-resource-adapter:activate
end-if

But if the parameter is false, I receive this error when deploying the EAR file:

{"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"WFLYCTL0412: Required services that are not installed:" => ["jboss.ra.my-resource-adapter"],"WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.deployment.subunit.\"myear.ear\".\"myear.jar\".component.MyMessageListener.CREATE is missing [jboss.ra.my-resource-adapter]"]}}}

Is there a way for the @ResourceAdapter annotation to not fail when the resource-adapter is not configured/ activated?

Thanks!

Expected Result: If the java code is left as it is and no resource adapter was added via CLI, then the EAR should still deploy and not cause the WFLYCTL0062 error

2

There are 2 answers

1
p3rl. On

to disable the @ResourceAdapter annotation in Wildfly when the resource adapter is not configured or activated, you can make use of the @Resource annotation instead.

here's how you can modify your code:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "useJndi", propertyValue = "true"),
    @ActivationConfigProperty(propertyName = "destination",
        propertyValue = "java:/jms/queue/myqueue"),
})
public class MyMessageListener implements MessageListener {

    @Resource(lookup = "java:/jms/queue/myqueue")
    private Queue queue;

    @Override
    public void onMessage(Message message) {
        // Use the queue here
    }
}

by using the @Resource annotation with the lookup attribute, you can directly inject the required resource (in this case, the JMS queue) without explicitly specifying the resource adapter.

this way, if the resource adapter is not configured or activated, the deployment of the EAR file will not fail. The @Resource annotation will simply leave the injected resource as null in that case.

note that you will need to remove the @ResourceAdapter("my-resource-adapter") annotation from your code.

by using this approach, you can control the usage of the resource adapter based on your boolean property (use_resource_adapter). If the property is set to true, you can configure and activate the resource adapter via CLI as before. If the property is set to false, you can omit the resource adapter configuration and the deployment will still succeed without any errors.

0
ehsavoie On

Note that the @Resource is about injection while @ResourceAdapter is about which resource adapter the MDB is going to use so this is an 'internal' usage. I think it will fallback to the default resource adapter. The solution would be to remove your MDB from your deployment. Your use case is no very clear to me. This can be achieved by exploding your ear and removing the MDB then redeploying with the jboss-cli. It might be put in your if block