An open API service indexing awesome lists of open source software.

https://github.com/tharindu714/programmatical-timer-j2ee

A Java EE application showcasing the use of programmatic timers via the TimerService API.
https://github.com/tharindu714/programmatical-timer-j2ee

j2ee java timer

Last synced: 5 months ago
JSON representation

A Java EE application showcasing the use of programmatic timers via the TimerService API.

Awesome Lists containing this project

README

          

# ⏱️ Programmatic Timer EJB Demo

> A Java EE application showcasing the use of programmatic timers via the `TimerService` API.

---

## πŸ“‘ Table of Contents

1. [✨ Project Overview](#-project-overview)
2. [πŸ“‚ Source Structure](#-source-structure)
3. [πŸ› οΈ Technologies & Dependencies](#️-technologies--dependencies)
4. [πŸš€ Getting Started](#-getting-started)

* [Prerequisites](#-prerequisites)
* [Clone & Build](#-clone--build)
* [Deploy & Run](#-deploy--run)
5. [πŸ”§ Configuration](#-configuration)
6. [πŸ“š Core Components](#-core-components)

* [TimerBean (EJB)](#timerbean-ejb)
* [REST Interface](#rest-interface)
7. [πŸ“„ API Endpoints](#-api-endpoints)
8. [πŸ”„ Timer Workflow](#-timer-workflow)
9. [πŸ›‘οΈ Error Handling & Logging](#️-error-handling--logging)
10. [🀝 Contributing](#-contributing)
11. [πŸ“œ License](#-license)

---

## ✨ Project Overview

This sample J2EE application illustrates creating and managing **programmatic timers** using the `javax.ejb.TimerService` interface. It provides:

* Dynamic timer creation with custom durations
* Callback handling via `@Timeout`
* A lightweight REST interface to start and inspect timers

Primarily intended for educational purposes, this demo helps you understand how to integrate timer-based tasks into your enterprise beans.

---

## πŸ“‚ Source Structure

```
Programmatical-Timer-J2EE/
β”œβ”€β”€ src/main/java/
β”‚ └── com/tharindu/ee/timer
β”‚ β”œβ”€β”€ ejb/
β”‚ β”œβ”€β”€ TaskSessionBean.java
β”‚ β”œβ”€β”€ TimerNewSessionBean.java
β”‚ β”œβ”€β”€ TimerSessionBean.java
β”‚ β”œβ”€β”€ servlet/
β”‚ β”œβ”€β”€ Test.java
β”‚ β”œβ”€β”€ TimerTest.java
β”‚ β”œβ”€β”€ CancelTimer.java
└── src/main/resources/
└── META-INF/
```

> *Note:* Package names (`com.tharindu.ee.timer`) should reflect your own domain.

---

## πŸ› οΈ Technologies & Dependencies

* **Java EE / Jakarta EE** (EJB, JAX-RS)
* **Maven** for build & dependency management
* **Application Server**: WildFly, GlassFish, or any JEE container with EJB support
* **JSON-B / Jackson** for JSON serialization
* **JUnit & Arquillian** (optional) for integration testing

---

## πŸš€ Getting Started

### Prerequisites

* **JDK 11+**
* **Maven 3.6+**
* **Java EE–compliant server** (WildFly 21+, GlassFish 5+)
* **Git**

### Clone & Build

```bash
# Clone the repo
git clone https://github.com/Tharindu714/Programmatical-Timer-J2EE.git
cd Programmatical-Timer-J2EE

# Build with Maven
mvn clean package
```

### Deploy & Run

1. **Deploy** the generated WAR (`target/timer.war`) to your server’s `deployments/` folder.
2. **Start** (or restart) the server.
3. **Access** the REST API at `http://localhost:8080/timer-demo/api/timer`.

---

## πŸ“š Core Components

### TimerBean (EJB)

* **Injection:** `@Resource private TimerService timerService;`
* **Create Timer:** `timerService.createSingleActionTimer(duration, new TimerConfig(info, false));`
* **Timeout Callback:** Method annotated `@Timeout` handles expiration events.

### REST Interface

* **TimerResource.java:** Defines endpoints to:

* **POST** `/api/timer` β†’ Create a new timer (JSON payload with `duration`).
* **GET** `/api/timer/{id}` β†’ Retrieve timer info (status, next timeout).

---

## πŸ“„ API Endpoints

| Method | Endpoint | Description |
| ------ | ----------------- | ------------------------------------------- |
| POST | `/api/timer` | Create a timer (body: `{"duration":60000}`) |
| GET | `/api/timer/{id}` | Get timer details and info |

---

## πŸ”„ Timer Workflow

1. **Client** sends POST with desired duration.
2. **TimerBean** uses `TimerService` to schedule a `SingleActionTimer`.
3. Upon expiration, the **@Timeout** method executes business logic (e.g., logging).
4. **Client** can query status via GET endpoint.

---

## πŸ›‘οΈ Error Handling & Logging

* **Validation:** Check payload duration is positive; return `400 Bad Request` if invalid.
* **ExceptionMapper:** Converts exceptions into JSON error responses.
* **Logging:** Standard `java.util.logging` in `logging.properties`.

---

## 🀝 Contributing

We welcome improvements!

1. **Fork** the repository.
2. **Create a branch:** `git checkout -b feature/xyz`.
3. **Commit** your changes with clear messages.
4. **Open** a Pull Request against `main`.

Please ensure Java EE best practices and update this README if structure changes.

---

## πŸ“œ License

MIT Β© 2025 Tharindu714

---

> Demonstrating enterprise timers, one callback at a time! β˜•οΈβœ¨