Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.stitch.operations;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.component.stitch.StitchConfiguration;
import org.apache.camel.component.stitch.StitchConstants;
import org.apache.camel.component.stitch.client.StitchClient;
import org.apache.camel.component.stitch.client.models.StitchMessage;
import org.apache.camel.component.stitch.client.models.StitchRequestBody;
import org.apache.camel.component.stitch.client.models.StitchResponse;
import org.apache.camel.component.stitch.client.models.StitchSchema;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
public class StitchProducerOperations {
private static final Logger LOG = LoggerFactory.getLogger(StitchProducerOperations.class);
private final StitchClient client;
private final StitchConfiguration configuration;
public StitchProducerOperations(StitchClient client, StitchConfiguration configuration) {
ObjectHelper.notNull(client, "client");
ObjectHelper.notNull(configuration, "configuration");
this.client = client;
this.configuration = configuration;
}
public boolean sendEvents(
final Message inMessage, final Consumer resultCallback, final AsyncCallback callback) {
sendAsyncEvents(inMessage)
.subscribe(resultCallback, error -> {
// error but we continue
if (LOG.isDebugEnabled()) {
LOG.debug("Error processing async exchange with error: {}", error.getMessage());
}
inMessage.getExchange().setException(error);
callback.done(false);
}, () -> {
// we are done from everything, so mark it as sync done
LOG.trace("All events with exchange have been sent successfully.");
callback.done(false);
});
return false;
}
private Mono sendAsyncEvents(final Message inMessage) {
return client.batch(createStitchRequestBody(inMessage));
}
@SuppressWarnings("unchecked")
// visible for testing
public StitchRequestBody createStitchRequestBody(final Message inMessage) {
if (inMessage.getBody() instanceof StitchRequestBody) {
return createStitchRequestBodyFromStitchRequestBody(inMessage.getBody(StitchRequestBody.class), inMessage);
}
if (inMessage.getBody() instanceof StitchMessage) {
return createStitchRequestBodyFromStitchMessages(Collections.singletonList(inMessage.getBody(StitchMessage.class)),
inMessage);
}
if (inMessage.getBody() instanceof Iterable) {
return createStitchRequestBodyFromIterable(inMessage.getBody(Iterable.class), inMessage);
}
if (inMessage.getBody() instanceof Map) {
return createStitchRecordFromMap(inMessage.getBody(Map.class), inMessage);
}
throw new IllegalArgumentException("Message body data `" + inMessage.getBody() + "` type is not supported");
}
private StitchRequestBody createStitchRequestBodyFromStitchRequestBody(
final StitchRequestBody requestBody, final Message message) {
return createStitchRecordFromBuilder(StitchRequestBody.fromStitchRequestBody(requestBody), message);
}
private StitchRequestBody createStitchRequestBodyFromStitchMessages(
final Collection stitchMessages, final Message message) {
final StitchRequestBody.Builder builder = StitchRequestBody.builder()
.addMessages(stitchMessages);
return createStitchRecordFromBuilder(builder, message);
}
@SuppressWarnings("unchecked")
private StitchRequestBody createStitchRequestBodyFromIterable(final Iterable