https://github.com/oswaldobapvicjr/agents
A lightweight timer/cron agents framework for Java applications
https://github.com/oswaldobapvicjr/agents
agent cron cron-jobs cronjob-scheduler crontab executor java scheduler timer timer-agent
Last synced: 5 months ago
JSON representation
A lightweight timer/cron agents framework for Java applications
- Host: GitHub
- URL: https://github.com/oswaldobapvicjr/agents
- Owner: oswaldobapvicjr
- License: apache-2.0
- Created: 2021-03-28T16:04:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-04-15T21:23:01.000Z (6 months ago)
- Last Synced: 2025-05-08T22:17:52.844Z (5 months ago)
- Topics: agent, cron, cron-jobs, cronjob-scheduler, crontab, executor, java, scheduler, timer, timer-agent
- Language: Java
- Homepage:
- Size: 266 KB
- Stars: 4
- Watchers: 1
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# agents
[](https://snyk.io/test/github/oswaldobapvicjr/agents)
[](https://github.com/oswaldobapvicjr/agents/actions/workflows/maven.yml)
[](https://codecov.io/gh/oswaldobapvicjr/agents)
[](https://maven-badges.herokuapp.com/maven-central/net.obvj/agents)
[](https://javadoc.io/doc/net.obvj/agents)A lightweight timer/cron agents framework for Java applications.
---
## Agents overview
Convert any Java class into an Agent by adding the **@Agent** annotation at the class, then mark one method with the **@Run** annotation so it will be called automatically. The framework looks up for agent classes by scanning user-specified packages at runtime and can run particular tasks periodically in the JVM.
An agent can be of type **Timer** or **Cron**:
### Timer agents
A Timer agent can be executed periodically, in a fixed run frequency, which must be in seconds, minutes, or hours. For example:
```java
package com.mycompany.agents;
...
@Agent(type = AgentType.TIMER, interval = "30 seconds", modulate = true)
public class MyTimerAgent {
@Run
public void execute() {
// This method will be called every 30 seconds...
}
}
```### Cron agents
A Cron agent can be executed at specific dates and times, comparable to the Cron Service available in Unix/Linux systems. Although they are more robust in terms of configuration flexibility, the interval between executions cannot be lower than 1 minute.
Cron systax has five fields separated by a space, and each field represent a unit of time.
```bash
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * *
```For example, the following agent is configured to execute every weekday at 2:00 AM:
```java
package com.mycompany.agents;
...
@Agent(type = AgentType.CRON, interval = "0 2 * * MON-FRI")
public class MyCronAgent {
@Run
public void execute() {
// This method will be called every weekday at 2:00 AM
}
}
```---
## Usage
1. Scan one or more base packages to search for agents
```java
AgentManager manager = AgentManager.defaultInstance();
manager.scanPackage("com.mycompany.agents");
```3. Start all agents
```java
manager.startAllAgents();
```---
## How to include it
If you are using Maven, add **Agents** as a dependency on your pom.xml file:
```xml
net.obvj
agents
0.3.3```
If you use other dependency managers (such as Gradle, Grape, Ivy, etc.) click [here](https://maven-badges.herokuapp.com/maven-central/net.obvj/agents).