Skip to content

CSCI44092GroupProject/notification-service

Repository files navigation

Notification Service

One of three microservices in the CSCI 44092 e-commerce backend. It listens to order events on RabbitMQ and logs a mock notification. No database — it only consumes messages and logs.

Role in the system

Order Service  --(publishes OrderCreatedEvent)-->  RabbitMQ  -->  Notification Service (logs notification)

The Order Service publishes an event to the order.created.queue queue when an order is created. This service consumes those messages and logs the notification details.

Tech stack

  • Java 21, Spring Boot
  • Spring AMQP (RabbitMQ)
  • springdoc-openapi (Swagger UI)
  • JUnit 5 + Mockito for tests

Configuration

Credentials are externalized via environment variables (with local-friendly defaults):

Variable Default Used by Description
RABBITMQ_HOST localhost app RabbitMQ host
RABBITMQ_PORT 5672 app + compose RabbitMQ port
RABBITMQ_USER myuser app + compose RabbitMQ username
RABBITMQ_PASS secret app + compose RabbitMQ password
RABBITMQ_MGMT_PORT 15672 compose RabbitMQ management UI host port

Both the app and Docker Compose read these from a .env file. Copy the template and adjust:

cp .env.example .env

.env is gitignored so real secrets are never committed. Docker Compose loads .env automatically. The Spring app falls back to the defaults above when a variable is unset. See application.properties and .env.example.

Running locally

RabbitMQ runs as its own container (compose.yaml) and the Notification Service runs as a separate container (compose.app.yaml). They share the notification-net Docker network, so the app reaches the broker by service name (RABBITMQ_HOST=rabbitmq).

  1. Start RabbitMQ (broker + management UI):

    docker compose -f compose.yaml up -d

    Management UI: http://localhost:15672 (user myuser, password secret).

  2. Build and start the Notification Service container:

    docker compose -f compose.app.yaml up -d --build

    The app is published on host port 8081 by default (host 8080 is often taken by a local Apache). Override with APP_PORT:

    APP_PORT=9090 docker compose -f compose.app.yaml up -d --build
  3. Tail the logs to watch notifications:

    docker compose -f compose.app.yaml logs -f notification-service

Publishing a test event

From the RabbitMQ container (or the management UI → Queues → order.created.queue → Publish):

docker exec notification-service-rabbitmq-1 rabbitmqadmin --username myuser --password secret \
  --non-interactive publish message -k order.created.queue \
  -m '{"orderId":1,"customerId":42,"productId":7,"productName":"Wireless Mouse","quantity":2,"totalPrice":59.98}'

Alternative: run the app on the host

Instead of step 2, you can run the jar directly with ./gradlew bootRun. Spring Boot's docker-compose support will auto-start compose.yaml if Docker is available. (This is dev-only; spring-boot-docker-compose is not packaged into the container image.)

Consumed message

Queue: order.created.queue

Expected JSON payload (OrderCreatedEvent):

{
  "orderId": 1,
  "customerId": 42,
  "productId": 7,
  "productName": "Wireless Mouse",
  "quantity": 2,
  "totalPrice": 59.98
}

When a message is received, the service logs the notification (order id, customer id, message, timestamp). You can publish a test message manually from the RabbitMQ management UI (Queues → order.created.queue → Publish message).

Set content_type=application/json when publishing manually, or the message can't be deserialized.

API documentation (Swagger)

With the app running on host port 8081 (the default — see "Running locally"):

Endpoints:

  • GET /notifications/status — service health/status endpoint.

Testing

Run the unit tests (no Docker or RabbitMQ required):

./gradlew test          # Git Bash / Linux / macOS
.\gradlew test          # Windows PowerShell

The tests cover the notification service logic and the RabbitMQ listener (including error handling). The HTML report is written to build/reports/tests/test/index.html.

Integration with the Order Service

The Order Service is the publisher; this service is the consumer. For a step-by-step guide on cloning, running both containers, publishing events, and verifying the notification log, see docs/ORDER_SERVICE_INTEGRATION.md.

Project layout

src/main/java/com/pm/notificationservice/
├─ config/        RabbitMQConfig, OpenApiConfig, SecurityConfig
├─ controller/    NotificationStatusController  (status endpoint for Swagger)
├─ messaging/     OrderEventListener, OrderCreatedEvent
├─ model/         Notification
└─ service/       NotificationService
compose.yaml          RabbitMQ broker container
compose.app.yaml      Notification Service container
Dockerfile            Multi-stage build for the service image

About

Handles notifications and message passing communicates with other services.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors