-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SolaceIO: refactor to allow inheritance of BasicAuthSempClient (#32400)
* Refactored to allow inheritance and overriding of BasicAuthSempClient * Fix docs and use Map#computeIfAbsent with a lambda. * Fix integration test * Remove 'serializable' * Revert 'Remove 'serializable''
- Loading branch information
Showing
6 changed files
with
403 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
62 changes: 62 additions & 0 deletions
62
...io/solace/src/test/java/org/apache/beam/sdk/io/solace/it/BasicAuthMultipleSempClient.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.beam.sdk.io.solace.it; | ||
|
||
import com.google.api.client.http.HttpRequestFactory; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.apache.beam.sdk.io.solace.broker.BasicAuthSempClient; | ||
import org.apache.beam.sdk.io.solace.broker.SempBasicAuthClientExecutor; | ||
import org.apache.beam.sdk.util.SerializableSupplier; | ||
|
||
/** | ||
* Example class showing how the {@link BasicAuthSempClient} can be extended or have functionalities | ||
* overridden. In this case, the modified method is {@link | ||
* BasicAuthSempClient#getBacklogBytes(String)}, which queries multiple SEMP endpoints to collect | ||
* accurate backlog metrics. For usage, see {@link SolaceIOMultipleSempIT}. | ||
*/ | ||
public class BasicAuthMultipleSempClient extends BasicAuthSempClient { | ||
private final List<SempBasicAuthClientExecutor> sempBacklogBasicAuthClientExecutors; | ||
|
||
public BasicAuthMultipleSempClient( | ||
String mainHost, | ||
List<String> backlogHosts, | ||
String username, | ||
String password, | ||
String vpnName, | ||
SerializableSupplier<HttpRequestFactory> httpRequestFactorySupplier) { | ||
super(mainHost, username, password, vpnName, httpRequestFactorySupplier); | ||
sempBacklogBasicAuthClientExecutors = | ||
backlogHosts.stream() | ||
.map( | ||
host -> | ||
new SempBasicAuthClientExecutor( | ||
host, username, password, vpnName, httpRequestFactorySupplier.get())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public long getBacklogBytes(String queueName) throws IOException { | ||
long backlog = 0; | ||
for (SempBasicAuthClientExecutor client : sempBacklogBasicAuthClientExecutors) { | ||
backlog += client.getBacklogBytes(queueName); | ||
} | ||
return backlog; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...ce/src/test/java/org/apache/beam/sdk/io/solace/it/BasicAuthMultipleSempClientFactory.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.beam.sdk.io.solace.it; | ||
|
||
import com.google.api.client.http.HttpRequestFactory; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.auto.value.AutoValue; | ||
import java.util.List; | ||
import org.apache.beam.sdk.io.solace.broker.SempClient; | ||
import org.apache.beam.sdk.io.solace.broker.SempClientFactory; | ||
import org.apache.beam.sdk.util.SerializableSupplier; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** | ||
* Example class showing how to implement a custom {@link SempClientFactory} with custom client. For | ||
* usage, see {@link SolaceIOMultipleSempIT}. | ||
*/ | ||
@AutoValue | ||
public abstract class BasicAuthMultipleSempClientFactory implements SempClientFactory { | ||
|
||
public abstract String mainHost(); | ||
|
||
public abstract List<String> backlogHosts(); | ||
|
||
public abstract String username(); | ||
|
||
public abstract String password(); | ||
|
||
public abstract String vpnName(); | ||
|
||
public abstract @Nullable SerializableSupplier<HttpRequestFactory> httpRequestFactorySupplier(); | ||
|
||
public static Builder builder() { | ||
return new AutoValue_BasicAuthMultipleSempClientFactory.Builder(); | ||
} | ||
|
||
@AutoValue.Builder | ||
public abstract static class Builder { | ||
/** Set Solace host, format: [Protocol://]Host[:Port]. */ | ||
public abstract Builder mainHost(String host); | ||
|
||
public abstract Builder backlogHosts(List<String> hosts); | ||
|
||
/** Set Solace username. */ | ||
public abstract Builder username(String username); | ||
/** Set Solace password. */ | ||
public abstract Builder password(String password); | ||
|
||
/** Set Solace vpn name. */ | ||
public abstract Builder vpnName(String vpnName); | ||
|
||
abstract Builder httpRequestFactorySupplier( | ||
SerializableSupplier<HttpRequestFactory> httpRequestFactorySupplier); | ||
|
||
public abstract BasicAuthMultipleSempClientFactory build(); | ||
} | ||
|
||
@Override | ||
public SempClient create() { | ||
return new BasicAuthMultipleSempClient( | ||
mainHost(), | ||
backlogHosts(), | ||
username(), | ||
password(), | ||
vpnName(), | ||
getHttpRequestFactorySupplier()); | ||
} | ||
|
||
@SuppressWarnings("return") | ||
private @NonNull SerializableSupplier<HttpRequestFactory> getHttpRequestFactorySupplier() { | ||
SerializableSupplier<HttpRequestFactory> httpRequestSupplier = httpRequestFactorySupplier(); | ||
return httpRequestSupplier != null | ||
? httpRequestSupplier | ||
: () -> new NetHttpTransport().createRequestFactory(); | ||
} | ||
} |
Oops, something went wrong.