#1
If you’re building a Java web app with Struts and you need reliable messaging—for example, handling async jobs, notifications, or stream processing—RabbitMQ is an excellent choice. This guide walks you through configuring RabbitMQ in a Struts-based project: using Docker for a fast local dev setup and Kubernetes for secure production deployments. You'll also learn how to manage configurations cleanly, without hardcoding values.

1. Add Essential Dependencies in pom.xml

First, add the needed libraries:
<dependencies>
  <!-- Struts core framework -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts.version}</version>
  </dependency>

  <!-- RabbitMQ Java client -->
  <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>${rabbitmq.version}</version>
  </dependency>

  <!-- Kubernetes client (optional for deployment integration) -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>${fabric8.version}</version>
  </dependency>
</dependencies>
This setup includes Struts, the RabbitMQ client, and Kubernetes integration for later use.

2. Run RabbitMQ with Docker for Local Development

Create a docker-compose.yml:
version: '3'
services:
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
Run this with:
docker-compose up -d
Now you have RabbitMQ running on localhost:5672, and the web UI accessible at http://localhost:15672 (default guest/guest login).

3. Local Dev Setup: rabbitmq-dev.properties

Create this in your classpath:
rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.queue=dev-queue
In your Struts utility or action class, load and use these settings:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/rabbitmq-dev.properties"));

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(props.getProperty("rabbitmq.host"));
factory.setPort(Integer.parseInt(props.getProperty("rabbitmq.port")));
factory.setUsername(props.getProperty("rabbitmq.username"));
factory.setPassword(props.getProperty("rabbitmq.password"));

Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
channel.queueDeclare(props.getProperty("rabbitmq.queue"), true, false, false, null);
Publish a message:
channel.basicPublish("", props.getProperty("rabbitmq.queue"), null, "Hello".getBytes());
Now you're good to test messaging locally!

4. Prepare Kubernetes for Production

To avoid leaking credentials, store values in Kubernetes:
kubectl create configmap rabbitmq-config \
  --from-literal=RABBITMQ_HOST=rabbitmq-service \
  --from-literal=RABBITMQ_PORT=5672 \
  --from-literal=RABBITMQ_QUEUE=prod-queue

kubectl create secret generic rabbitmq-secret \
  --from-literal=RABBITMQ_USERNAME=produser \
  --from-literal=RABBITMQ_PASSWORD=prodpass

5. Production Setup: rabbitmq-prod.properties

Place this in your resources:
rabbitmq.host=${RABBITMQ_HOST}
rabbitmq.port=${RABBITMQ_PORT}
rabbitmq.username=${RABBITMQ_USERNAME}
rabbitmq.password=${RABBITMQ_PASSWORD}
rabbitmq.queue=${RABBITMQ_QUEUE}
Struts or your config loader should pick up environment variables at runtime.

6. Kubernetes Deployment Snippet

Ensure your Pod spec includes:
env:
  - name: RABBITMQ_HOST
    valueFrom:
      configMapKeyRef:
        name: rabbitmq-config
        key: RABBITMQ_HOST
  - name: RABBITMQ_PORT
    valueFrom:
      configMapKeyRef:
        name: rabbitmq-config
        key: RABBITMQ_PORT
  - name: RABBITMQ_QUEUE
    valueFrom:
      configMapKeyRef:
        name: rabbitmq-config
        key: RABBITMQ_QUEUE
  - name: RABBITMQ_USERNAME
    valueFrom:
      secretKeyRef:
        name: rabbitmq-secret
        key: RABBITMQ_USERNAME
  - name: RABBITMQ_PASSWORD
    valueFrom:
      secretKeyRef:
        name: rabbitmq-secret
        key: RABBITMQ_PASSWORD
Deploy your Struts app packaged with rabbitmq-prod.properties. It will automatically use Kubernetes-provided values.

7. Why This Approach Works

  • Quick dev setup: Docker and rabbitmq-dev.properties let you iterate fast.
  • Secure deployment: No credentials in code—Kubernetes ensures safety.
  • Code consistency: Same Struts logic works in both environments.
  • Clear separation: Dev vs prod kept clean through separate files.
By combining Docker for local RabbitMQ testing, Struts for your web app, and Kubernetes for production-grade deployments, you end up with a messaging-ready system that’s easy to use and maintain. Whether you’re sending logs, events, or notifications, your Struts-based service is now equipped to handle RabbitMQ messaging reliably—no matter the environment.

image quote pre code