MultithreadApiCaller is a robust Java application designed to efficiently manage and execute multiple API calls concurrently. This project provides a flexible framework for calling various external APIs, processing their responses, and storing the results in different formats (JSON or CSV). It's perfect for scenarios where you need to regularly poll multiple APIs and aggregate their data.
- 🔌 Multiple API Support: Built-in support for three different APIs:
- UrlShortener: Shortens long URLs
- HarryPotter: Fetches random Harry Potter universe data (characters, houses, spells, books)
- Fixer: Retrieves currency exchange rates from Fixer.io (requires API key)
- ⚡ Concurrent Execution: Smart thread pooling with configurable concurrency limits
- 📊 Multiple Output Formats: Store results in beautifully formatted JSON or structured CSV
- ⏰ Scheduled Polling: Regular API calls with configurable intervals
- 🔒 Graceful Shutdown: Proper resource cleanup and task completion handling
- 📝 Comprehensive Logging: Detailed logging for monitoring and debugging
| Technology | Purpose |
|---|---|
| Java 21 | Core programming language |
| Apache HttpClient 5 | HTTP client for API communications |
| SLF4J & Logback | Logging framework |
| Jackson | JSON processing library |
| JUnit 5 | Unit testing framework |
| Mockito | Mocking framework for tests |
| Lombok | Reduced boilerplate code |
MultithreadApiCaller/
├── src/main/java/org/berezneva/apicaller/
│ ├── service/
│ │ ├── ApiCaller.java # Interface for all API callers
│ │ ├── Fixer.java # Fixer.io currency API implementation
│ │ ├── HarryPotter.java # Harry Potter API implementation
│ │ ├── UrlShortener.java # URL shortener API implementation
│ │ └── Services.java # Service enumeration
│ ├── ApiManager.java # Main orchestration and scheduling
│ ├── DataParser.java # Output formatting (JSON/CSV)
│ └── Main.java # Application entry point
├── src/test/java/org/berezneva/apicaller/
│ ├── service/
│ │ ├── ApiCallerTest.java
│ │ ├── FixerTest.java
│ │ ├── HarryPotterTest.java
│ │ └── UrlShortenerTest.java
│ ├── testUtils/
│ │ └── Utils.java
│ ├── ApiManagerTest.java
│ ├── DataParserCsvTest.java
│ ├── DataParserJsonTest.java
│ ├── DataParserTest.java
│ └── MainTest.java
└── resources/ # .env file right here
- Java 21 (prefered)
- Gradle 8.8 (for building, prefered)
- Fixer.io API key (for Fixer service)
-
Clone the repository:
git clone https://github.com/your-username/MultithreadApiCaller.git cd MultithreadApiCaller -
Configure environment variables: In
.envfile in the root directory:FIXER_API_KEY=your_fixer_api_key_here
-
Build the project:
./gradlew build
Run the application with custom parameters:
java -jar multithreadapicaller.jar <maxThreads> <timeoutSeconds> <services> <format>Parameters:
maxThreads: Maximum number of concurrent threads (e.g.,5)timeoutSeconds: Interval between API calls in seconds (e.g.,10)services: Comma-separated list of services (e.g.,UrlShortener,HarryPotter,Fixer)format: Output format (jsonorcsv)
Example:
java -jar multithreadapicaller.jar 3 15 UrlShortener,HarryPotter jsonIf no arguments are provided, the application uses sensible defaults:
- Max Threads: 5
- Timeout: 10 seconds
- Services: All available services
- Format: JSON
- Shortens long URLs using spoo.me service
- Default URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ (Rick Astley - Never Gonna Give You Up)
- Returns: JSON with shortened URL
- Fetches random Harry Potter data from potterapi-fedeperin.vercel.app
- Randomly selects from: characters, houses, spells, or books
- Limits results to 3 items per call
- Returns: JSON array of items
- Retrieves currency exchange rates from Fixer.io
- Requires: FIXER_API_KEY environment variable
- Returns: Latest exchange rates in JSON format
// ===== SERVICENAME ===== //
{
"data": "beautifully formatted",
"with": "proper indentation"
}### NEW API DATA ###
Service: SERVICENAME
field1,field2,field3
value1,value2,value3
Output files are saved in the res/ directory as parse.json or parse.csv.
FIXER_API_KEY: Your Fixer.io API key (required for Fixer service)
Configure through command line arguments or use defaults:
- Thread pool size
- API call interval
- Enabled services
- Output format
The project includes comprehensive unit tests (up to 90% of all functions are covered). Test coverage includes:
- API client initialization and error handling
- HTTP request/response mocking
- JSON/CSV output formatting
- Concurrent execution scenarios
- Edge cases and error conditions
This is a project created to showcase multithreading, API integration, and concurrent programming patterns in Java. While fully functional, it's primarily intended for educational purposes and as a foundation for more complex production systems.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Thank you for checking out MultithreadApiCaller! 🎉 Happy coding! 💻✨