Skip to content

Commit

Permalink
actions poc
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 7, 2023
1 parent ecb80d4 commit f172771
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 120 deletions.
37 changes: 37 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

/**
* Kafka Action.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.2.5
*/
public interface Action {

/**
* Apply action.
*/
void apply();
}
79 changes: 79 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/AssignPartitions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.cactoos.list.ListOf;

import java.util.Collection;

/**
* Assign Partitions for Kafka Consumer.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.2.5
*/
public final class AssignPartitions implements Action {

/**
* Consumer.
*/
private final KafkaConsumer<?, ?> consumer;
/**
* Partitions to assign.
*/
private final Collection<TopicPartition> partitions;

/**
* Ctor.
*
* @param cnsmr Kafka Consumer
* @param prts Partitions to assign
*/
public AssignPartitions(
final KafkaConsumer<?, ?> cnsmr,
final Collection<TopicPartition> prts
) {
this.consumer = cnsmr;
this.partitions = prts;
}

/**
* Ctor.
*
* @param cnsmr Kafka Consumer
* @param prts Partitions to assign.
*/
public AssignPartitions(
final KafkaConsumer<?, ?> cnsmr,
final TopicPartition... prts
) {
this(cnsmr, new ListOf<>(prts));
}

@Override
public void apply() {
this.consumer.assign(this.partitions);
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/CommitAsync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
* Async Offset Commit.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.2.5
*/
@RequiredArgsConstructor
public final class CommitAsync implements Action {

/**
* Consumer.
*/
private final KafkaConsumer<?, ?> consumer;

@Override
public void apply() {
this.consumer.commitAsync();
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/CommitSync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
* Sync Offset Commit.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.2.5
*/
@RequiredArgsConstructor
public final class CommitSync implements Action {

/**
* Consumer.
*/
private final KafkaConsumer<?, ?> consumer;

@Override
public void apply() {
this.consumer.commitSync();
}
}
58 changes: 58 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/ShutdownHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

import lombok.RequiredArgsConstructor;

/**
* Shutdown Hook.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.2.5
*/
@RequiredArgsConstructor
public final class ShutdownHook implements Action {

/**
* Action.
*/
private final Action action;
/**
* Main thread.
*/
private final Thread main;

@Override
public void apply() {
Runtime.getRuntime().addShutdownHook(
new Thread(() -> {
this.action.apply();
try {
this.main.join();
} catch (final InterruptedException ex) {
ex.printStackTrace();
}
})
);
}
}
47 changes: 47 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/act/Wakeup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.act;

import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
* Wakeup of Kafka Consumer.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @see ShutdownHook
* @since 0.2.5
*/
@RequiredArgsConstructor
public final class Wakeup implements Action {

/**
* Consumer.
*/
private final KafkaConsumer<?, ?> consumer;

@Override
public void apply() {
this.consumer.wakeup();
}
}
10 changes: 0 additions & 10 deletions src/main/java/io/github/eocqrs/kafka/ext/Action.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/io/github/eocqrs/kafka/ext/AssignPartitions.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/io/github/eocqrs/kafka/ext/CommitAsync.java

This file was deleted.

Loading

0 comments on commit f172771

Please sign in to comment.