|
1 |
| -# sdk-java |
| 1 | +# CDEvents Java SDK |
| 2 | + |
| 3 | +Java SDK to produce [CDEvents](https://cdevents.dev). |
| 4 | + |
| 5 | +The SDK can be used to create CDEvents and render as CloudEvents to send them to a specific CloudEvents broker |
| 6 | + |
| 7 | +## Add dependency module |
| 8 | + |
| 9 | +```xml |
| 10 | +<dependency> |
| 11 | + <groupId>dev.cdevents</groupId> |
| 12 | + <artifactId>cdevents-sdk-java</artifactId> |
| 13 | + <version>${cdevents.version}</version> |
| 14 | +</dependency> |
| 15 | +``` |
| 16 | + |
| 17 | +## Create your first CDEvent |
| 18 | + |
| 19 | +Below is the example of creating a new [PipelineRun-finished](https://cdevents.dev/docs/core/#pipelinerun-finished) event, |
| 20 | + |
| 21 | +```java |
| 22 | +public class CDEventsExample { |
| 23 | + |
| 24 | + public static void main(String args[]){ |
| 25 | + /*when creating new object of any CDEvent type, the event will be initialized with |
| 26 | + context.id, context.type, context.version, context.timestamp |
| 27 | + and subject.type */ |
| 28 | + PipelineRunFinishedCDEvent pipelineRunFinishedCDEvent = new PipelineRunFinishedCDEvent(); |
| 29 | + |
| 30 | + /* set the required context fields to the pipelineRunFinishedCDEvent */ |
| 31 | + pipelineRunFinishedCDEvent.setSource(URI.create("http://dev.cdevents")); |
| 32 | + |
| 33 | + /* set the required subject fields to the pipelineRunFinishedCDEvent */ |
| 34 | + pipelineRunFinishedCDEvent.setSubjectId("/dev/pipeline/run/1"); |
| 35 | + pipelineRunFinishedCDEvent.setSubjectSource(URI.create("http://dev.pipeline.run/source")); |
| 36 | + pipelineRunFinishedCDEvent.setSubjectUrl(URI.create("http://dev.pipeline.run/url")); |
| 37 | + pipelineRunFinishedCDEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS); |
| 38 | + pipelineRunFinishedCDEvent.setSubjectPipelineName("testPipeline"); |
| 39 | + pipelineRunFinishedCDEvent.setSubjectErrors("pipelineErrors"); |
| 40 | + |
| 41 | + /* Create a CloudEvent from a pipelineRunFinishedCDEvent */ |
| 42 | + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(pipelineRunFinishedCDEvent); |
| 43 | + |
| 44 | + /* This CDEvent can be sent as CloudEvent using HTTP Protocol Binding, |
| 45 | + Refer : https://cloudevents.github.io/sdk-java/http-basic.html |
| 46 | + */ |
| 47 | + |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +Now the CDEvent can be sent as CloudEvent using [Generic HTTP Protocol Binding](https://cloudevents.github.io/sdk-java/http-basic.html) |
| 52 | + |
| 53 | +## Contributing |
| 54 | + |
| 55 | +If you would like to contribute, see our [development](DEVELOPMENT.md) guide. |
| 56 | + |
| 57 | +## References |
| 58 | + |
| 59 | +- [CDEvents](https://cdevents.dev) |
| 60 | +- [CDFoundation SIG Events Vocabulary Draft](https://github.com/cdfoundation/sig-events/tree/main/vocabulary-draft) |
0 commit comments