in publish-events/channel-connection/client-libraries/java/src/main/java/com/google/cloud/eventarc/publishing/example/PublishEventsExample.java [79:100]
private void SendEventUsingProtoFormat(String channelConnectionName, CloudEvent event) throws Exception {
byte[] serializedEvent = EventFormatProvider.getInstance()
.resolveFormat(ProtobufFormat.PROTO_CONTENT_TYPE)
.serialize(event);
io.cloudevents.v1.proto.CloudEvent protoEvent = io.cloudevents.v1.proto.CloudEvent.parseFrom(serializedEvent);
Any wrappedEvent = Any.pack(protoEvent);
PublishChannelConnectionEventsRequest request = PublishChannelConnectionEventsRequest.newBuilder()
.setChannelConnection(channelConnectionName)
.addEvents(wrappedEvent)
.build();
LOGGER.log(Level.INFO, "Publishing message in Eventarc");
try {
// Create a client with credentials provided by the system.
PublisherClient client = PublisherClient.create();
PublishChannelConnectionEventsResponse response = client.publishChannelConnectionEvents(request);
LOGGER.log(Level.INFO, String.format("Message published successfully.\nReceived response: %s",
response.toString()));
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "An exception occurred while publishing", ex);
}
}