oscerd commented on code in PR #13630: URL: https://github.com/apache/camel/pull/13630#discussion_r1542340660
########## components/camel-google/camel-google-pubsub-lite/src/main/java/org/apache/camel/component/google/pubsublite/GooglePubsubLiteProducer.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.apache.camel.component.google.pubsublite; + +import java.util.List; +import java.util.Map; + +import com.google.api.core.ApiFuture; +import com.google.cloud.pubsublite.cloudpubsub.Publisher; +import com.google.common.base.Strings; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.PubsubMessage; +import org.apache.camel.Exchange; +import org.apache.camel.support.DefaultProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.component.google.pubsublite.GooglePubsubLiteConstants.ATTRIBUTES; +import static org.apache.camel.component.google.pubsublite.GooglePubsubLiteConstants.ORDERING_KEY; +import static org.apache.camel.component.google.pubsublite.GooglePubsubLiteConstants.RESERVED_GOOGLE_CLIENT_ATTRIBUTE_PREFIX; + +/** + * Generic PubSub Lite Producer + */ +public class GooglePubsubLiteProducer extends DefaultProducer { + + public Logger logger; + + public GooglePubsubLiteProducer(GooglePubsubLiteEndpoint endpoint) { + super(endpoint); + + String loggerId = endpoint.getLoggerId(); + + if (Strings.isNullOrEmpty(loggerId)) { + loggerId = this.getClass().getName(); + } + + logger = LoggerFactory.getLogger(loggerId); + } + + /** + * The incoming message is expected to be either - a List of Exchanges (aggregated) - an Exchange + */ + @Override + public void process(Exchange exchange) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug("uploader thread/id: {} / {}. api call completed.", Thread.currentThread().getId(), + exchange.getExchangeId()); + } + + if (exchange.getIn().getBody() instanceof List) { + boolean groupedExchanges = false; + for (Object body : exchange.getIn().getBody(List.class)) { + if (body instanceof Exchange) { + send((Exchange) body); + groupedExchanges = true; + } + } + if (!groupedExchanges) { + send(exchange); + } + } else { + send(exchange); + } + } + + private void send(Exchange exchange) throws Exception { + + GooglePubsubLiteEndpoint endpoint = (GooglePubsubLiteEndpoint) getEndpoint(); + String topicName = String.format("projects/%s/locations/%s/topics/%s", endpoint.getProjectId(), endpoint.getLocation(), + endpoint.getDestinationName()); + + Publisher publisher = endpoint.getComponent().getPublisher(topicName, endpoint); + + Object body = exchange.getIn().getBody(); + ByteString byteString; + + if (body instanceof String) { + byteString = ByteString.copyFromUtf8((String) body); + } else if (body instanceof byte[]) { + byteString = ByteString.copyFrom((byte[]) body); + } else { + byteString = ByteString.copyFrom(endpoint.getSerializer().serialize(body)); + } + + PubsubMessage.Builder messageBuilder = PubsubMessage.newBuilder().setData(byteString); + Map<String, String> attributes = exchange.getIn().getHeader(ATTRIBUTES, Map.class); + if (attributes != null) { + for (Map.Entry<String, String> attribute : attributes.entrySet()) { + if (!attribute.getKey().startsWith(RESERVED_GOOGLE_CLIENT_ATTRIBUTE_PREFIX)) { + messageBuilder.putAttributes(attribute.getKey(), attribute.getValue()); + } + } + } + String orderingKey = exchange.getIn().getHeader(ORDERING_KEY, String.class); + if (orderingKey != null) { + messageBuilder.setOrderingKey(orderingKey); + } + + PubsubMessage message = messageBuilder.build(); + + ApiFuture<String> messageIdFuture = publisher.publish(message); + exchange.getIn().setHeader(GooglePubsubLiteConstants.MESSAGE_ID, messageIdFuture.get()); Review Comment: Use getMessage instead of getIn ########## components/camel-google/camel-google-pubsub-lite/src/main/java/org/apache/camel/component/google/pubsublite/GooglePubsubLiteComponent.java: ########## @@ -0,0 +1,245 @@ +/* + * 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.apache.camel.component.google.pubsublite; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.pubsub.v1.MessageReceiver; +import com.google.cloud.pubsub.v1.stub.PublisherStubSettings; +import com.google.cloud.pubsublite.SubscriptionPath; +import com.google.cloud.pubsublite.TopicPath; +import com.google.cloud.pubsublite.cloudpubsub.*; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import org.apache.camel.Endpoint; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.annotations.Component; +import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.ResourceHelper; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents the component that manages {@link GooglePubsubLiteEndpoint}. + */ +@Component("google-pubsub-lite") +public class GooglePubsubLiteComponent extends DefaultComponent { + private static final Logger LOG = LoggerFactory.getLogger(GooglePubsubLiteComponent.class); + + @Metadata(label = "common", + description = "The Service account key that can be used as credentials for the PubSub Lite publisher/subscriber. It can be loaded by default from " + + " classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems.") + private String serviceAccountKey; + + @Metadata( + label = "producer", + description = "Maximum number of producers to cache. This could be increased if you have producers for lots of different topics.") + private int publisherCacheSize = 100; + + @Metadata( + label = "producer", + description = "How many milliseconds should each producer stay alive in the cache.") + private int publisherCacheTimeout = 180000; + + @Metadata( + label = "consumer", + description = "How many milliseconds should each producer stay alive in the cache. " + + "Must be greater than the allowed size of the largest message (1 MiB).") + private long consumerBytesOutstanding = 10 * 1024 * 1024; + + @Metadata( + label = "consumer", + description = "The number of messages that may be outstanding to the client. Must be >0.") + private long consumerMessagesOutstanding = 1000; + + @Metadata( + label = "advanced", + description = "How many milliseconds should a producer be allowed to terminate.") + private int publisherTerminationTimeout = 60000; + + private RemovalListener<String, Publisher> removalListener = removal -> { + Publisher publisher = removal.getValue(); + if (publisher == null) { Review Comment: To check for null you can use the ObjectHelper from camel. ########## components/camel-google/camel-google-pubsub-lite/src/main/java/org/apache/camel/component/google/pubsublite/GooglePubsubLiteComponent.java: ########## @@ -0,0 +1,245 @@ +/* + * 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.apache.camel.component.google.pubsublite; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.pubsub.v1.MessageReceiver; +import com.google.cloud.pubsub.v1.stub.PublisherStubSettings; +import com.google.cloud.pubsublite.SubscriptionPath; +import com.google.cloud.pubsublite.TopicPath; +import com.google.cloud.pubsublite.cloudpubsub.*; Review Comment: Please don't use star imports. ########## components/camel-google/camel-google-pubsub-lite/.gitignore: ########## @@ -0,0 +1,7 @@ +.idea Review Comment: This file could be removed. ########## components/camel-google/camel-google-pubsub-lite/src/main/java/org/apache/camel/component/google/pubsublite/GooglePubsubLiteEndpoint.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.apache.camel.component.google.pubsublite; + +import java.util.concurrent.ExecutorService; + +import org.apache.camel.*; +import org.apache.camel.component.google.pubsublite.serializer.DefaultGooglePubsubSerializer; +import org.apache.camel.component.google.pubsublite.serializer.GooglePubsubSerializer; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.apache.camel.support.DefaultEndpoint; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Send and receive messages to/from Google Cloud Platform PubSub Lite Service. + * <p/> + * Built on top of the Google Cloud Pub/Sub Lite libraries. + */ +@UriEndpoint(firstVersion = "4.5.0", scheme = "google-pubsub-lite", title = "Google Pubsub Lite", + syntax = "google-pubsub-lite:projectId:location:destinationName", + category = { Category.CLOUD, Category.MESSAGING }, + headersClass = GooglePubsubLiteConstants.class) +public class GooglePubsubLiteEndpoint extends DefaultEndpoint { + + private Logger log; + + @UriPath(label = "common", description = "The Google Cloud PubSub Lite Project Id") + @Metadata(required = true) + private Long projectId; + + @UriPath(label = "common", description = "The Google Cloud PubSub Lite location") + @Metadata(required = true) + private String location; + + @UriPath(label = "common", + description = "The Destination Name. For the consumer this will be the subscription name, while for the producer this will be the topic name.") + @Metadata(required = true) + private String destinationName; + + @UriParam(label = "common", + description = "The Service account key that can be used as credentials for the PubSub publisher/subscriber. It can be loaded by default from " + + " classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems.") + private String serviceAccountKey; + + @UriParam(name = "loggerId", description = "Logger ID to use when a match to the parent route required") + private String loggerId; + + @UriParam(label = "consumer", name = "concurrentConsumers", + description = "The number of parallel streams consuming from the subscription", + defaultValue = "1") + private Integer concurrentConsumers = 1; + + @UriParam(label = "consumer", name = "maxMessagesPerPoll", + description = "The max number of messages to receive from the server in a single API call", defaultValue = "1") + private Integer maxMessagesPerPoll = 1; + + @UriParam(label = "consumer", defaultValue = "AUTO", enums = "AUTO,NONE", + description = "AUTO = exchange gets ack'ed/nack'ed on completion. NONE = downstream process has to ack/nack explicitly") + private GooglePubsubLiteConstants.AckMode ackMode = GooglePubsubLiteConstants.AckMode.AUTO; + + @UriParam(label = "consumer", name = "maxAckExtensionPeriod", + description = "Set the maximum period a message ack deadline will be extended. Value in seconds", + defaultValue = "3600") + private int maxAckExtensionPeriod = 3600; + + @UriParam(description = "Pub/Sub endpoint to use. Required when using message ordering, and ensures that messages are received in order even when multiple publishers are used", + label = "producer,advanced") + private String pubsubEndpoint; + + @UriParam(name = "serializer", + description = "A custom GooglePubsubLiteSerializer to use for serializing message payloads in the producer", + label = "producer,advanced") + @Metadata(autowired = true) + private GooglePubsubSerializer serializer; + + public GooglePubsubLiteEndpoint(String uri, Component component) { + super(uri, component); + + if (!(component instanceof GooglePubsubLiteComponent)) { Review Comment: I think this is a bit overkilling, we could avoid this check. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org