-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample to create the client via dynamic proxy
- Loading branch information
Hadi Eskandari
committed
Feb 15, 2024
1 parent
61f51a7
commit 1c6d1b5
Showing
5 changed files
with
141 additions
and
4 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
58 changes: 58 additions & 0 deletions
58
samples/src/main/java/org/opensearch/client/samples/OpenSearchClientProxy.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,58 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.samples; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import net.sf.cglib.proxy.Enhancer; | ||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.client.transport.OpenSearchTransport; | ||
|
||
public class OpenSearchClientProxy implements MethodInterceptor { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(OpenSearchClientProxy.class); | ||
|
||
public static OpenSearchClient create(OpenSearchTransport transport) throws Exception { | ||
if (transport == null) { | ||
throw new Exception("Cannot build OpenSearchClient without a transport."); | ||
} | ||
|
||
LOGGER.info("Using {} transport", transport.getClass().getName()); | ||
|
||
final var interceptor = new OpenSearchClientProxy(); | ||
final var enhancer = new Enhancer(); | ||
|
||
enhancer.setSuperclass(OpenSearchClient.class); | ||
enhancer.setCallback(interceptor); | ||
|
||
final var argTypes = new Class[] { OpenSearchTransport.class }; | ||
final var args = new Object[] { transport }; | ||
return (OpenSearchClient) enhancer.create(argTypes, args); | ||
} | ||
|
||
@Override | ||
public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy proxy) throws Throwable { | ||
LOGGER.info("Invoking method: {}", method.getName()); | ||
final Object result; | ||
|
||
try { | ||
LOGGER.info("Entering method: {}", method.getName()); | ||
result = proxy.invokeSuper(obj, args); | ||
} catch (final InvocationTargetException e) { | ||
throw e.getTargetException(); | ||
} finally { | ||
LOGGER.info("Exiting method: {}", method.getName()); | ||
} | ||
return result; | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
samples/src/main/java/org/opensearch/client/samples/SampleProxyClient.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,73 @@ | ||
package org.opensearch.client.samples; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.opensearch._types.mapping.IntegerNumberProperty; | ||
import org.opensearch.client.opensearch._types.mapping.Property; | ||
import org.opensearch.client.opensearch._types.mapping.TypeMapping; | ||
import org.opensearch.client.opensearch.core.IndexRequest; | ||
import org.opensearch.client.opensearch.core.SearchResponse; | ||
import org.opensearch.client.opensearch.indices.CreateIndexRequest; | ||
import org.opensearch.client.opensearch.indices.DeleteIndexRequest; | ||
import org.opensearch.client.opensearch.indices.IndexSettings; | ||
import org.opensearch.client.samples.util.IndexData; | ||
|
||
/** | ||
* Run with: <c>./gradlew :samples:run -Dsamples.mainClass=SampleProxyClient</c> | ||
*/ | ||
public class SampleProxyClient { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(SampleProxyClient.class); | ||
|
||
public static void main(String[] args) { | ||
try { | ||
var transport = SampleClient.createTransport(); | ||
var client = OpenSearchClientProxy.create(transport); | ||
|
||
final var indexName = "my-index"; | ||
|
||
if (!client.indices().exists(r -> r.index(indexName)).value()) { | ||
LOGGER.info("Creating index {}", indexName); | ||
IndexSettings settings = new IndexSettings.Builder().numberOfShards("2").numberOfReplicas("1").build(); | ||
TypeMapping mapping = new TypeMapping.Builder().properties( | ||
"age", | ||
new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build() | ||
).build(); | ||
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName) | ||
.settings(settings) | ||
.mappings(mapping) | ||
.build(); | ||
client.indices().create(createIndexRequest); | ||
} | ||
|
||
LOGGER.info("Indexing documents"); | ||
IndexData indexData = new IndexData("Document 1", "Text for document 1"); | ||
IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(indexName) | ||
.id("1") | ||
.document(indexData) | ||
.build(); | ||
client.index(indexRequest); | ||
|
||
indexData = new IndexData("Document 2", "Text for document 2"); | ||
indexRequest = new IndexRequest.Builder<IndexData>().index(indexName).id("2").document(indexData).build(); | ||
client.index(indexRequest); | ||
|
||
// wait for the document to index | ||
Thread.sleep(3000); | ||
|
||
SearchResponse<IndexData> searchResponse = client.search(s -> s.index(indexName), IndexData.class); | ||
for (var hit : searchResponse.hits().hits()) { | ||
LOGGER.info("Found {} with score {}", hit.source(), hit.score()); | ||
} | ||
|
||
LOGGER.info("Deleting document with id 1"); | ||
client.delete(d -> d.index(indexName).id("1")); | ||
|
||
LOGGER.info("Deleting index {}", indexName); | ||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(indexName).build(); | ||
client.indices().delete(deleteIndexRequest); | ||
} catch (Exception e) { | ||
LOGGER.error("Unexpected exception", e); | ||
} | ||
} | ||
} |