-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28207e2
commit 952f62c
Showing
12 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ctions/runtime/src/main/java/io/quarkus/funqy/gcp/functions/FunqyCloudEventsFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.funqy.gcp.functions; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import com.google.cloud.functions.CloudEventsFunction; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.quarkus.runtime.Application; | ||
|
||
public class FunqyCloudEventsFunction implements CloudEventsFunction { | ||
protected static final String deploymentStatus; | ||
protected static boolean started = false; | ||
|
||
static { | ||
StringWriter error = new StringWriter(); | ||
PrintWriter errorWriter = new PrintWriter(error, true); | ||
if (Application.currentApplication() == null) { // were we already bootstrapped? Needed for mock unit testing. | ||
// For GCP functions, we need to set the TCCL to the QuarkusHttpFunction classloader then restore it. | ||
// Without this, we have a lot of classloading issues (ClassNotFoundException on existing classes) | ||
// during static init. | ||
ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); | ||
try { | ||
Thread.currentThread().setContextClassLoader(FunqyCloudEventsFunction.class.getClassLoader()); | ||
Class<?> appClass = Class.forName("io.quarkus.runner.ApplicationImpl"); | ||
String[] args = {}; | ||
Application app = (Application) appClass.getConstructor().newInstance(); | ||
Runtime.getRuntime().addShutdownHook(new Thread() { | ||
@Override | ||
public void run() { | ||
app.stop(); | ||
} | ||
}); | ||
app.start(args); | ||
errorWriter.println("Quarkus bootstrapped successfully."); | ||
started = true; | ||
} catch (Exception ex) { | ||
errorWriter.println("Quarkus bootstrap failed."); | ||
ex.printStackTrace(errorWriter); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(currentCl); | ||
} | ||
} else { | ||
errorWriter.println("Quarkus bootstrapped successfully."); | ||
started = true; | ||
} | ||
deploymentStatus = error.toString(); | ||
} | ||
|
||
@Override | ||
public void accept(CloudEvent cloudEvent) throws Exception { | ||
if (!started) { | ||
throw new RuntimeException(deploymentStatus); | ||
} | ||
FunqyCloudFunctionsBindingRecorder.handle(cloudEvent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-functions/runtime/src/main/java/io/quarkus/gcp/functions/QuarkusCloudEventsFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.quarkus.gcp.functions; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import com.google.cloud.functions.CloudEventsFunction; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.quarkus.arc.Arc; | ||
import io.quarkus.runtime.Application; | ||
|
||
public final class QuarkusCloudEventsFunction implements CloudEventsFunction { | ||
|
||
protected static final String deploymentStatus; | ||
protected static boolean started = false; | ||
|
||
private static volatile CloudEventsFunction delegate; | ||
|
||
static { | ||
StringWriter error = new StringWriter(); | ||
PrintWriter errorWriter = new PrintWriter(error, true); | ||
if (Application.currentApplication() == null) { // were we already bootstrapped? Needed for mock unit testing. | ||
ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); | ||
try { | ||
// For GCP functions, we need to set the TCCL to the QuarkusHttpFunction classloader then restore it. | ||
// Without this, we have a lot of classloading issues (ClassNotFoundException on existing classes) | ||
// during static init. | ||
Thread.currentThread().setContextClassLoader(QuarkusCloudEventsFunction.class.getClassLoader()); | ||
Class<?> appClass = Class.forName("io.quarkus.runner.ApplicationImpl"); | ||
String[] args = {}; | ||
Application app = (Application) appClass.getConstructor().newInstance(); | ||
app.start(args); | ||
errorWriter.println("Quarkus bootstrapped successfully."); | ||
started = true; | ||
} catch (Exception ex) { | ||
errorWriter.println("Quarkus bootstrap failed."); | ||
ex.printStackTrace(errorWriter); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(currentCl); | ||
} | ||
} else { | ||
errorWriter.println("Quarkus bootstrapped successfully."); | ||
started = true; | ||
} | ||
deploymentStatus = error.toString(); | ||
} | ||
|
||
static void setDelegate(String selectedDelegate) { | ||
if (selectedDelegate != null) { | ||
try { | ||
Class<?> clazz = Class.forName(selectedDelegate, false, Thread.currentThread().getContextClassLoader()); | ||
delegate = (CloudEventsFunction) Arc.container().instance(clazz).get(); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void accept(CloudEvent cloudEvent) throws Exception { | ||
if (!started) { | ||
throw new IOException(deploymentStatus); | ||
} | ||
|
||
// TODO maybe we can check this at static init | ||
if (delegate == null) { | ||
throw new IOException("We didn't found any CloudEventsFunction to run " + | ||
"(or there is multiple one and none selected inside your application.properties)"); | ||
} | ||
|
||
delegate.accept(cloudEvent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
integration-tests/funqy-google-cloud-functions/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
quarkus.funqy.export=helloGCSWorld | ||
#quarkus.funqy.export=helloGCSWorld | ||
#quarkus.funqy.export=helloPubSubWorld | ||
quarkus.funqy.export=helloCloudEvent |
26 changes: 26 additions & 0 deletions
26
...gle-cloud-functions/src/main/java/io/quarkus/gcp/function/test/CloudEventStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.quarkus.gcp.function.test; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import com.google.cloud.functions.CloudEventsFunction; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.quarkus.gcp.function.test.service.GreetingService; | ||
|
||
@Named("cloudEventTest") | ||
@ApplicationScoped | ||
public class CloudEventStorageTest implements CloudEventsFunction { | ||
@Inject | ||
GreetingService greetingService; | ||
|
||
@Override | ||
public void accept(CloudEvent cloudEvent) throws Exception { | ||
System.out.println("Receive event Id: " + cloudEvent.getId()); | ||
System.out.println("Receive event Subject: " + cloudEvent.getSubject()); | ||
System.out.println("Receive event Type: " + cloudEvent.getType()); | ||
System.out.println("Receive event Data: " + new String(cloudEvent.getData().toBytes())); | ||
System.out.println("Be polite, say " + greetingService.hello()); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
integration-tests/google-cloud-functions/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
quarkus.google-cloud-functions.function=httpTest | ||
#quarkus.google-cloud-functions.function=httpTest | ||
quarkus.google-cloud-functions.function=cloudEventTest | ||
#quarkus.google-cloud-functions.function=rawPubSubTest | ||
#quarkus.google-cloud-functions.function=storageTest |