blob: 15c5ce01de721817d5e74110622a61a816ce0b63 [file] [log] [blame]
= HTTP with vanilla JAX-RS or with Camel `platform-http` component: A Camel Quarkus example
:cq-example-description: An example that shows how to create HTTP endpoints using either the RESTEasy
implementation of JAX-RS or using Camel `platform-http` component.
{cq-description}
The Quarkus REST service `ExampleResource.java` and the Camel route `CamelRoute.java`
are independent and are present in this example to demonstrate how you can build microservices
with both Quarkus and Camel separated.
You can of course also combine Quarkus REST services with Camel (see further below).
TIP: Check the https://camel.apache.org/camel-quarkus/latest/first-steps.html[Camel Quarkus User guide] for prerequisites
and other general information.
This example was used in a 10 minute video recording to demonstrate
how to quickly run 100 Camels with Apache Camel, Quarkus and GraalVM:
- https://www.youtube.com/watch?v=4lXSf8DBQkQ
== Start in the Development mode
[source,shell]
----
$ mvn clean compile quarkus:dev
----
The above command compiles the project, starts the application and lets the Quarkus tooling watch for changes in your
workspace. Any modifications in your project will automatically take effect in the running application.
TIP: Please refer to the Development mode section of
https://camel.apache.org/camel-quarkus/latest/first-steps.html#_development_mode[Camel Quarkus User guide] for more details.
From a web browser, you can call the two services via
- http://localhost:8080/hello
- http://localhost:8080/camel/hello
There is also health check and metrics available from the following urls:
- http://localhost:8080/q/health
- http://localhost:8080/q/metrics
=== Package and run the application
Once you are done with developing you may want to package and run the application.
TIP: Find more details about the JVM mode and Native mode in the Package and run section of
https://camel.apache.org/camel-quarkus/latest/first-steps.html#_package_and_run_the_application[Camel Quarkus User guide]
==== JVM mode
[source,shell]
----
$ mvn clean package
$ java -jar target/quarkus-app/quarkus-run.jar
...
[io.quarkus] (main) camel-quarkus-examples-... started in 1.163s. Listening on: http://0.0.0.0:8080
----
==== Native mode
IMPORTANT: Native mode requires having GraalVM and other tools installed. Please check the Prerequisites section
of https://camel.apache.org/camel-quarkus/latest/first-steps.html#_prerequisites[Camel Quarkus User guide].
To prepare a native executable using GraalVM, run the following command:
[source,shell]
----
$ mvn clean package -Pnative
$ ./target/*-runner
...
[io.quarkus] (main) camel-quarkus-examples-... started in 0.013s. Listening on: http://0.0.0.0:8080
...
----
== Using Camel from Quarkus JAX-RS
The `ExampleResource.java` is a pure JAX-RS REST service without using Camel.
Suppose you wanted to add a HTTP POST that sends the HTTP body to a Kafka topic,
via the camel-kafka component. You can then integrate Quarkus
with Camel by dependency injecting Camels `FluentProducerTemplate` that allows to
send the message in one line of code to Kafka. What's left is to configure the URL to the Kafka brokers,
which can be done in the `application.properties` file.
[source,java]
----
import org.apache.camel.FluentProducerTemplate;
import org.jboss.resteasy.annotations.Body;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
@Path("/hello")
public class ExampleResource {
@Inject
FluentProducerTemplate producer;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
@POST
@Consumes(MediaType.TEXT_PLAIN)
public void foo(@Body String payload) {
producer.to("kafka:foo").send(payload);
}
}
----
== Feedback
Please report bugs and propose improvements via https://github.com/apache/camel-quarkus/issues[GitHub issues of Camel Quarkus] project.