Skip to content

A high-performance Java application for making parallel API calls to various services with scheduling capabilities, thread pooling, and multiple output formats support.

Notifications You must be signed in to change notification settings

fa11astro/MultithreadApiCaller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MultithreadApiCaller

Project Overview

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.


Key Features

  • 🔌 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

Technologies Used

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

Project Structure

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

Getting Started

Prerequisites

  • Java 21 (prefered)
  • Gradle 8.8 (for building, prefered)
  • Fixer.io API key (for Fixer service)

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/MultithreadApiCaller.git
    cd MultithreadApiCaller
  2. Configure environment variables: In .env file in the root directory:

    FIXER_API_KEY=your_fixer_api_key_here
  3. Build the project:

    ./gradlew build

Usage

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 (json or csv)

Example:

java -jar multithreadapicaller.jar 3 15 UrlShortener,HarryPotter json

Default Configuration

If no arguments are provided, the application uses sensible defaults:

  • Max Threads: 5
  • Timeout: 10 seconds
  • Services: All available services
  • Format: JSON

API Services

🔗 UrlShortener

🧙 HarryPotter

  • 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

💰 Fixer

  • Retrieves currency exchange rates from Fixer.io
  • Requires: FIXER_API_KEY environment variable
  • Returns: Latest exchange rates in JSON format

Output Formats

JSON Output

// ===== SERVICENAME ===== //
{
  "data": "beautifully formatted",
  "with": "proper indentation"
}

CSV Output

### 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.


Configuration

Environment Variables

  • FIXER_API_KEY: Your Fixer.io API key (required for Fixer service)

Runtime Configuration

Configure through command line arguments or use defaults:

  • Thread pool size
  • API call interval
  • Enabled services
  • Output format

Testing

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

Note

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.


Contributing

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! 💻✨

About

A high-performance Java application for making parallel API calls to various services with scheduling capabilities, thread pooling, and multiple output formats support.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages