activemq-artemis/examples/features/standard/cdi
Clebert Suconic 2be5c54cd0 [maven-release-plugin] prepare release 2.29.0 2023-06-14 18:20:45 -04:00
..
src/main ARTEMIS-756 Moving Client CDI as a main module 2016-10-24 12:12:18 -04:00
pom.xml [maven-release-plugin] prepare release 2.29.0 2023-06-14 18:20:45 -04:00
readme.md ARTEMIS-1562 Refactor example documentation 2017-12-15 14:54:16 +00:00

readme.md

CDI Example

This is a simple example that demonstrates how to use CDI to integrate with ActiveMQ Artemis on the client side. It is designed mainly for connecting to a remote broker rather than embedding within your application.

Configuring the connection

While the integration provides an out of the box solution for configuration with some sensible defaults, the values should be configurable. This example leverages Apache DeltaSpike to configure the connectivity. It overrides the username, password and URL for the broker. The configuration hard codes the connector class. This configuration class is a standard CDI bean.

@ApplicationScoped
public class CDIClientConfig implements ArtemisClientConfiguration {
   @Inject
   @ConfigProperty(name = "username")
   private String username;

   @Inject
   @ConfigProperty(name = "password")
   private String password;

   @Inject
   @ConfigProperty(name = "url")
   private String url;

   @Override
   public String getUsername() {
      return username;
   }

   @Override
   public String getPassword() {
      return password;
   }

   @Override
   public String getUrl() {
      return url;
   }

   @Override
   public String getConnectorFactory() {
      return "org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory";
   }
}

Setup and Tear Down

For the example, we leverage DeltaSpike's Container Control to start and stop the container. It is implemented within the main method, and is done in a way to make this work for both Weld and OpenWebBeans.

Sending and Receiving Messages

The key to how the CDI integration works is the built in beans. It provides two out of the box - a ConnectionFactory and a JMSContext. Most operations should be performed against the JMS 2.0 simplified API using JMSContext. The example does this via an observer method, but can be done anywhere.

@ApplicationScoped
public class CDIMessagingIntegrator {
   @Inject
   private JMSContext context;
   public void init(@Observes @Initialized(ApplicationScoped.class) Object obj) {
      String body = "This is a test";
      Queue queue = context.createQueue("test");
      context.createProducer().send(queue, body);
      String receivedBody = context.createConsumer(queue).receiveBody(String.class, 5000);
      System.out.println("Received a message "+receivedBody);
   }
}