Last active
October 12, 2018 17:46
-
-
Save evankanderson/8804872b6148ca9015bdc14fac9203f6 to your computer and use it in GitHub Desktop.
Knative simplest event source
This file contains hidden or 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
import com.fasterxml.jackson.databind.ObjectMapper; | |
import io.streamzi.cloudevents.CloudEvent; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import javax.enterprise.event.Event; | |
class DoTheThing implements Runnable { | |
// Set up class, etc | |
// ... | |
@Inject | |
@ExternalNotify | |
private Event<CloudEvent<MyCustomEvent>> cloudEvent; | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
@Override | |
void run() { | |
// Do all the existing stuff | |
// ... | |
// ... AND THEN (from https://github.com/project-streamzi/jcloudevents/blob/master/introduction.md): | |
CloudEvent<MyCustomEvent> event = new CloudEventBuilder<MyCustomEvent>() | |
.eventType("com.just-get-it-done.dept.DoTheThing") | |
.source(new URI("/DoTheThing")) | |
.eventID(operationID.toString()) | |
.build(); | |
cloudEvent.fire(event); | |
// Do the rest | |
} | |
public void sendEvent(@Observes @ExternalNotify CloudEvent event) /* throws ... */ { | |
String endpointUrl = System.getProperty("EVENT_ENDPOINT"); // The string from status.domain | |
String eventToken = System.getProperty("EVENT_TOKEN"); // The string from status.token | |
if (endpointUrl == null || eventToken == null) { | |
LOGGER.log("No endpoint configured, dropping event"); | |
return; | |
} | |
URL url = new URL(endpointUrl); | |
HttpUrlConnection conn = (HttpUrlConnection) url.openConnection(); | |
conn.setRequestMethod("POST") | |
// From https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#321-http-content-type | |
conn.setRequestProperty("Content-Type", "application/cloudevents+json; charset=UTF-8") | |
conn.setRequestProperty("Authorization", "Bearer " + eventToken) | |
conn.setDoOutput(true); | |
conn.connect(); | |
MAPPER.writeValue(conn.getOutputStream(), event); | |
if (conn.getResponseCode() >= HttpUrlConnection.HTTP_MULT_CHOICE) { | |
LOGGER.log("Got unexpected >200 response for message: " + MAPPER.writeValueAsString(event)); | |
} | |
} | |
//... | |
} |
This file contains hidden or 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
apiVersion: eventing.knative.dev/v1alpha1 | |
kind: EventType | |
metadata: | |
name: my-custom-event | |
spec: | |
schema: "object" | |
--- | |
apiVersion: eventing.knative.dev/v1alpha1 | |
kind: Source | |
metadata: | |
name: do-the-thing | |
annotations: | |
eventing.knative.dev/cluster-provisioner: simple-ingress | |
spec: | |
provisioner: | |
name: | |
apiVersion: eventing.knative.dev/v1alpha1 | |
kind: ClusterProvisioner | |
name: simple-ingress | |
arguments: | |
- name: external | |
value: True | |
- name: auth | |
value: token | |
# Filled in by server: | |
# status: | |
# conditions: | |
# - type: Ready | |
# value: True | |
# domain: do-the-thing-ab112c.my-namespace.eventing.just-get-it-done.com | |
# token: aa87UTe9bZ42Aorb |
I think you meant the simple-ingress controller implementation? The provisioner would look like:
apiVersion: eventing.knative.dev/v1alpha1
kind: ClusterProvisioner
metadata:
name: simple-ingress
spec:
type:
group: eventing.knative.dev
kind: Source
status:
conditions:
type: Ready
status: True
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it would help to also see the simple-ingress provisioner definition