Skip to content

Commit

Permalink
Support Quarkus command mode apache#1037
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jun 19, 2020
1 parent 1bdb772 commit a5f5f82
Show file tree
Hide file tree
Showing 23 changed files with 543 additions and 79 deletions.
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<module>timer-log</module>
<module>timer-log-cdi</module>
<module>timer-log-kotlin</module>
<module>timer-log-main</module>
<module>timer-log-xml</module>
<module>timer-log-spring</module>
<module>file-split-log-xml</module>
Expand Down
36 changes: 36 additions & 0 deletions examples/timer-log-cdi/src/main/java/org/acme/timer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acme.timer;

import javax.inject.Inject;

import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.apache.camel.quarkus.core.CamelRuntime;

@QuarkusMain
public class Main implements QuarkusApplication {
@Inject
CamelRuntime runtime;

@Override
public int run(String... args) throws Exception {
runtime.start(args, true);

return 0;
}
}
56 changes: 56 additions & 0 deletions examples/timer-log-main/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
= timer-log

This is a basic hello world example that uses a cusomt main to set-up
a Camel timer that triggers every second and prints to the log.

TIP: Check the https://camel.apache.org/camel-quarkus/latest/first-steps.html[Camel Quarkus User guide] for prerequisites
and other general information.

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

Then look at the log output in the console. As we run the example in Quarkus Dev Mode, you can edit the source code and have live updates.
For example try to change `Incremented the counter` to `Counter state` in the message body or change the property `timer.period` in application.properties

=== 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/*-runner.jar
...
[io.quarkus] (main) Quarkus 1.3.2 started in 1.163s. Listening on: http://[::]: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) Quarkus 1.3.2 started in 0.013s. Listening on: http://[::]:8080
...
----
90 changes: 90 additions & 0 deletions examples/timer-log-main/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-build-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../../poms/build-parent/pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-examples-timer-log-main</artifactId>
<name>Camel Quarkus :: Examples :: Timer Log Main</name>
<description>Camel Quarkus Example :: Timer to Log Main</description>

<properties>
<!-- mvnd, a.k.a. Maven Daemon: https://github.com/gnodet/mvnd -->
<!-- The following rule tells mvnd to build the listed deployment modules before this module. -->
<!-- This is important because mvnd builds modules in parallel by default. The deployment modules are not -->
<!-- explicit dependencies of this module in the Maven sense, although they are required by the Quarkus Maven plugin. -->
<!-- Please update rule whenever you change the dependencies of this module by running -->
<!-- mvn process-resources -Pformat from the root directory -->
<mvnd.builder.rule>camel-quarkus-log-deployment,camel-quarkus-support-policy-deployment,camel-quarkus-timer-deployment</mvnd.builder.rule>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-log</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<id>build</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>

</project>
28 changes: 28 additions & 0 deletions examples/timer-log-main/src/main/java/org/acme/timer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acme.timer;

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.apache.camel.quarkus.main.CamelMainApplication;

@QuarkusMain
public class Main {
public static void main(String... args) {
Quarkus.run(CamelMainApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acme.timer;

import javax.enterprise.context.ApplicationScoped;

import org.apache.camel.builder.RouteBuilder;

/**
* A simple {@link RouteBuilder}.
*/
@ApplicationScoped
public class TimerRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:foo?period={{timer.period}}")
.setBody().constant("Hello from Main!")
.to("log:example");
}
}
42 changes: 42 additions & 0 deletions examples/timer-log-main/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

#
# Quarkus
#
quarkus.banner.enabled = false

# camel look-up beans using BeanManager so we don't want
# ArC to remove beans without injection points.
quarkus.arc.remove-unused-beans = false

#
# Camel - Main
#
camel.main.duration-hit-exit-code = 15

#
# Camel - Components
#
camel.component.log.exchange-formatter = #class:org.apache.camel.support.processor.DefaultExchangeFormatter
camel.component.log.exchange-formatter.show-exchange-pattern = false
camel.component.log.exchange-formatter.show-body-type = false

#
# Integration
#
timer.period = 5000
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Produce;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.RawCommandLineArgumentsBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.runtime.ShutdownContext;
import org.apache.camel.quarkus.core.CamelBootstrapRecorder;
Expand All @@ -31,14 +32,24 @@ class CamelBootstrapProcessor {
/**
* Starts the given {@link CamelRuntimeBuildItem}.
*
* @param recorder the recorder.
* @param runtime a reference to the {@link CamelRuntimeBuildItem}.
* @param shutdown a reference to a {@link ShutdownContext} used tor register the Camel's related shutdown tasks.
* @param recorder the recorder.
* @param runtime a reference to the {@link CamelRuntimeBuildItem}.
* @param commandLineArguments a reference to the raw command line arguments as they were passed to the application.
* @param shutdown a reference to a {@link ShutdownContext} used tor register the Camel's related shutdown
* tasks.
*/
@BuildStep(onlyIf = { CamelConfigFlags.BootstrapEnabled.class })
@Record(value = ExecutionTime.RUNTIME_INIT)
@Produce(CamelBootstrapCompletedBuildItem.class)
void boot(CamelBootstrapRecorder recorder, CamelRuntimeBuildItem runtime, ShutdownContextBuildItem shutdown) {
recorder.start(shutdown, runtime.get());
void boot(
CamelBootstrapRecorder recorder,
CamelRuntimeBuildItem runtime,
RawCommandLineArgumentsBuildItem commandLineArguments,
ShutdownContextBuildItem shutdown) {

recorder.addShutdownTask(shutdown, runtime.runtime());
if (runtime.isAutoStartup()) {
recorder.start(runtime.runtime(), commandLineArguments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@

public final class CamelRuntimeBuildItem extends SimpleBuildItem {
private final RuntimeValue<CamelRuntime> runtime;
private final boolean autoStartup;

public CamelRuntimeBuildItem(RuntimeValue<CamelRuntime> runtime) {
this(runtime, true);
}

public CamelRuntimeBuildItem(RuntimeValue<CamelRuntime> runtime, boolean autoStartup) {
this.runtime = runtime;
this.autoStartup = autoStartup;
}

public RuntimeValue<CamelRuntime> get() {
public RuntimeValue<CamelRuntime> runtime() {
return runtime;
}

public boolean isAutoStartup() {
return autoStartup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
*/
package org.apache.camel.quarkus.core;

import java.util.function.Supplier;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import org.jboss.logging.Logger;

@Recorder
public class CamelBootstrapRecorder {
public void start(ShutdownContext shutdown, RuntimeValue<CamelRuntime> runtime) {
public void addShutdownTask(ShutdownContext shutdown, RuntimeValue<CamelRuntime> runtime) {
shutdown.addShutdownTask(new Runnable() {
@Override
public void run() {
Expand All @@ -34,12 +36,13 @@ public void run() {
}
}
});
}

public void start(RuntimeValue<CamelRuntime> runtime, Supplier<String[]> arguments) {
try {
Logger logger = Logger.getLogger(CamelBootstrapRecorder.class);
logger.infof("bootstrap runtime: %s", runtime.getValue().getClass().getName());

runtime.getValue().start();
runtime.getValue().start(arguments.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit a5f5f82

Please sign in to comment.