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.
- Host: GitHub
- URL: https://github.com/tharindu714/programmatical-timer-j2ee
- Owner: Tharindu714
- Created: 2025-06-14T10:47:12.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-06-14T11:24:38.000Z (7 months ago)
- Last Synced: 2025-06-14T11:34:53.034Z (7 months ago)
- Topics: j2ee, java, timer
- Language: Java
- Homepage:
- Size: 0 Bytes
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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! βοΈβ¨