简体   繁体   English

使用 Java 中的 Azure Cosmos DB 发布/订阅的示例

[英]Sample for publish/subscribe with Azure Cosmos DB in Java

I need a pub/sub event message system with Azure Cosmos DB.我需要一个带有 Azure Cosmos DB 的发布/订阅事件消息系统。 I use Azure Cosmos DB Java SDK v4.我使用 Azure Cosmos DB Java SDK v4。

I try with a ChangeFeedProcessor based on this sample https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/main/src/main/java/com/azure/cosmos/examples/changefeed/SampleChangeFeedProcessor.java but it does not work like expected.我尝试使用基于此示例https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/main/src/main/java/com/azure/cosmos/examples的 ChangeFeedProcessor /changefeed/SampleChangeFeedProcessor.java但它不像预期的那样工作。

My problems:我的问题:

  • The feed collection/container grow continue.饲料收集/容器继续增长。 How can I delete an event after all active nodes have receive the event?在所有活动节点都收到事件后,如何删除事件?
  • The delay for the events seems relative large.事件的延迟似乎相对较大。 Around a minute.大约一分钟。
  • Only one node receive the events.只有一个节点接收事件。 This seems interesting for load balancing but this is not my use case.这对于负载平衡来说似乎很有趣,但这不是我的用例。

With version 4.12.0 of the Java SDK the follow code snipped works for me.使用 Java SDK 的 4.12.0 版本,以下代码对我有用。 But it use beta code from the driver.但它使用来自驱动程序的测试代码。 It can change in the future.它可以在未来改变。

private static final String                CHANNEL = "events";

private CosmosContainer                    collection;

private boolean                            stopped;

void start( String clientID ) {
    CosmosContainerProperties props = new CosmosContainerProperties( CHANNEL, "/type" );
    // delete all events after 60 seconds. All nodes should receive it in the meantime.
    props.setDefaultTimeToLiveInSeconds( 60 );
    collection = getOrCreateContainer( props );
    Thread thread = new Thread( () -> {
        String[] continuation = new String[1];
        try {
            while( !stopped ) {
                // sample code: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerChangeFeedTest.java
                CosmosChangeFeedRequestOptions options = continuation[0] == null ? //
                CosmosChangeFeedRequestOptions.createForProcessingFromNow( FeedRange.forFullRange() ) : // initial value
                CosmosChangeFeedRequestOptions.createForProcessingFromContinuation( continuation[0] ); // continue value
                Iterator<EventPOJO> it = collection //
                                .queryChangeFeed( options, EventPOJO.class ) //
                                .handle( ( response ) -> continuation[0] = response.getContinuationToken() ) //
                                .iterator();
                while( it.hasNext() ) {
                    EventPOJO event = it.next();
                    if( event.client != clientID ) {
                        // filter the own events
                        onMessage( event );
                    }
                }
                // poll interval
                Thread.sleep( 1000 );
            }
        } catch( Throwable th ) {
            if( !stopped ) {
                PersistenceLogger.LOGGER.error( th );
            }
        }
    }, CHANNEL );
    thread.setDaemon( true );
    thread.start();
}

<T> void send( T event, String clientID ) {
    EventPOJO evt = new EventPOJO();
    evt.id = ...
    evt.client = clientID;
    evt.type = event.getClass().getName();
    evt.message = ...

    collection.createItem( evt );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM