-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Kasean/25-create-file-service
First version of artifacts service implementation
- Loading branch information
Showing
50 changed files
with
1,377 additions
and
106 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
archiver/src/main/java/org/student/messaging/MessageProducer.java
This file was deleted.
Oops, something went wrong.
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
63 changes: 61 additions & 2 deletions
63
artifact-processor/src/main/java/org/student/Application.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 |
---|---|---|
@@ -1,7 +1,66 @@ | ||
package org.student; | ||
|
||
import org.student.api.factories.ConsumerFactory; | ||
import org.student.api.managers.ConsumersManager; | ||
import org.student.api.managers.ConsumersManagerImpl; | ||
import org.student.configs.ApplicationConfig; | ||
import org.student.api.consumers.MessageConsumer; | ||
import org.student.services.ArtifactsService; | ||
import org.student.services.ArtifactsServiceImpl; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.env.EnvScalarConstructor; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.yaml.snakeyaml.env.EnvScalarConstructor.ENV_FORMAT; | ||
import static org.yaml.snakeyaml.env.EnvScalarConstructor.ENV_TAG; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
System.out.println("Hello world!"); | ||
|
||
private static final AtomicReference<ConsumersManager> consumersManager = new AtomicReference<>(null); | ||
|
||
public static void main(String[] args) throws IOException { | ||
|
||
if (args.length != 1) { | ||
System.err.println("Config missing."); | ||
return; | ||
} | ||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
System.out.println("Shutdown hook is running!"); | ||
|
||
if (consumersManager.get() != null){ | ||
consumersManager.get().shutdown(); | ||
} | ||
|
||
})); | ||
|
||
ApplicationConfig config = loadConfig(args[0]); | ||
|
||
new Thread(() -> { | ||
ArtifactsService artifactsService = new ArtifactsServiceImpl(config); | ||
|
||
var consumers = ConsumerFactory.createConsumers(config.getKafka(), artifactsService); | ||
|
||
if (consumersManager.get() == null) { | ||
consumersManager.set(new ConsumersManagerImpl(consumers)); | ||
consumersManager.get().startListenMessages(); | ||
} | ||
}).start(); | ||
} | ||
|
||
private static ApplicationConfig loadConfig(String configFile) throws IOException { | ||
Yaml yaml = new Yaml(new EnvScalarConstructor()); | ||
yaml.addImplicitResolver(ENV_TAG, ENV_FORMAT, "$"); | ||
|
||
ApplicationConfig config; | ||
try (InputStream in = Files.newInputStream(Paths.get(configFile))) { | ||
config = yaml.loadAs(in, ApplicationConfig.class); | ||
} | ||
|
||
return config; | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
artifact-processor/src/main/java/org/student/api/consumers/DeleteConsumer.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,49 @@ | ||
package org.student.api.consumers; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.student.api.utils.ProcessMessageFunction; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.Properties; | ||
import java.util.UUID; | ||
|
||
public class DeleteConsumer implements MessageConsumer{ | ||
private final KafkaConsumer<String, String> consumer; | ||
private final ProcessMessageFunction<String, UUID, String> service; | ||
private final String topic; | ||
|
||
public DeleteConsumer(String bootstrapServers, String groupId, String topic, ProcessMessageFunction<String, UUID, String> service) { | ||
Properties properties = new Properties(); | ||
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
this.consumer = new KafkaConsumer<>(properties); | ||
|
||
this.service = service; | ||
this.topic = topic; | ||
} | ||
|
||
@Override | ||
public void consume() { | ||
TopicPartition partition = new TopicPartition(topic, 0); | ||
consumer.assign(Collections.singletonList(partition)); | ||
|
||
while (true) { | ||
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); | ||
if (!records.isEmpty()) { | ||
for (ConsumerRecord<String, String> record : records) { | ||
System.out.println("Consumed message: " + record.value()); | ||
service.accept(record.key(), UUID.fromString(record.value()), topic); | ||
} | ||
} else | ||
System.out.println("No messages"); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
artifact-processor/src/main/java/org/student/api/consumers/MessageConsumer.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,6 @@ | ||
package org.student.api.consumers; | ||
|
||
public interface MessageConsumer { | ||
|
||
void consume(); | ||
} |
31 changes: 18 additions & 13 deletions
31
...rg/student/messaging/MessageConsumer.java → ...g/student/api/consumers/ReadConsumer.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 |
---|---|---|
@@ -1,44 +1,49 @@ | ||
package org.student.messaging; | ||
package org.student.api.consumers; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.student.api.utils.ProcessMessageFunction; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Properties; | ||
import java.util.UUID; | ||
|
||
public class MessageConsumer { | ||
public class ReadConsumer implements MessageConsumer{ | ||
private final KafkaConsumer<String, String> consumer; | ||
private final ProcessMessageFunction<String, UUID, String> service; | ||
private final String topic; | ||
|
||
private final KafkaConsumer<String, byte[]> consumer; | ||
|
||
public MessageConsumer(String bootstrapServers, String groupId) { | ||
public ReadConsumer(String bootstrapServers, String groupId, String topic, ProcessMessageFunction<String, UUID, String> service) { | ||
Properties properties = new Properties(); | ||
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class); | ||
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
this.consumer = new KafkaConsumer<>(properties); | ||
|
||
this.service = service; | ||
this.topic = topic; | ||
} | ||
|
||
public void consume(String topic) { | ||
@Override | ||
public void consume() { | ||
TopicPartition partition = new TopicPartition(topic, 0); | ||
consumer.assign(Collections.singletonList(partition)); | ||
|
||
while (true) { | ||
ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofMillis(100)); | ||
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); | ||
if (!records.isEmpty()) { | ||
for (ConsumerRecord<String, byte[]> record : records) { | ||
System.out.println("Consumed message: " + new String(record.value())); | ||
for (ConsumerRecord<String, String> record : records) { | ||
System.out.println("Consumed message: " + record.value()); | ||
service.accept(record.key(), UUID.fromString(record.value()), topic); | ||
} | ||
} else | ||
System.out.println("No messages"); | ||
} | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
artifact-processor/src/main/java/org/student/api/consumers/UpdateConsumer.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,32 @@ | ||
package org.student.api.consumers; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.student.configs.ApplicationConfig; | ||
import org.student.services.ArtifactsService; | ||
import org.student.services.ArtifactsServiceImpl; | ||
|
||
import java.util.Properties; | ||
|
||
public class UpdateConsumer implements MessageConsumer{ | ||
private final KafkaConsumer<String, byte[]> consumer; | ||
private final ArtifactsService artifactsService; | ||
|
||
public UpdateConsumer(ApplicationConfig config) { | ||
Properties properties = new Properties(); | ||
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, config.getKafka().getBootstrapServers()); | ||
properties.put(ConsumerConfig.GROUP_ID_CONFIG, config.getKafka().getGroupId()); | ||
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class); | ||
this.consumer = new KafkaConsumer<>(properties); | ||
|
||
this.artifactsService = new ArtifactsServiceImpl(config); | ||
} | ||
|
||
@Override | ||
public void consume() { // Not implemented | ||
|
||
} | ||
} |
Oops, something went wrong.