Your task definition is where you'll perform your tasks, such as sending emails,
processing files, or moving data between systems.With Mergent, tasks are defined by HTTP request handlers inside your
application.
Application.java
Copy
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RestController public class TaskController { @PostMapping("/api/tasks") public ResponseEntity<String> performTask(@RequestBody String body) { System.out.println("Performing task: " + body); return new ResponseEntity<>("", HttpStatus.OK); } }}
For a list of all the available parameters, visit the API
Reference.
Before you can run your task, your handler must be accessible from the internet.To do this, you can use a tool like ngrok, or deploy your
application to a platform like Vercel or Render.For more information, see our guide to localhost dev &
webhooks.
Once your task handler is reachable via a URL, you can create your first task.
Go to the Mergent Tasks Dashboard and click the
Create button.
In this step, you'll set the Request URL to your task handler's URL. You'll also
provide the required parameters (in the request body) for your handler to carry
out the task.
Typically, developers use a type parameter to distinguish between different
tasks. This allows your task handler to route and process tasks accordingly.
Feel free to structure your parameters in a way that works best for your
application.
Click Create to queue the task. Once the task executes, you should see the
related log in your console, indicating a successful run. 🎊
Create a task whenever you need to. For example, you might create a task
any time a user signs up.Use the URL of the task definition you'd like to run.
Copy
import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.net.http.HttpHeaders;import java.time.Duration;public class Main { public static void main(String[] args) throws Exception { // set the Mergent API key String apiKey = "..."; String url = "https://api.mergent.co/v2/tasks"; // create a task that will run in 5 minutes String requestBody = "{ \"request\": { \"url\": \"...\", \"body\": \"Hello, world!\" }, \"delay\": \"PT5M\" }"; HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(new URI(url)) .header("Authorization", "Bearer " + apiKey) .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println("Response: " + response.body()); }}
You can now create a task any time you'd like to perform some work in the
background. 🎊