-
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 #8 from skodjob/redis
Add Redis endpoint and fix parent paths
- Loading branch information
Showing
8 changed files
with
166 additions
and
2 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
44 changes: 44 additions & 0 deletions
44
database-manipulation-tool/src/main/java/io/skodjob/dmt/resource/RedisResource.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,44 @@ | ||
package io.skodjob.dmt.resource; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
import io.skodjob.dmt.service.RedisService; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.QueryParam; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import redis.clients.jedis.resps.StreamEntry; | ||
|
||
@Path("Redis") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@ApplicationScoped | ||
@RegisterForReflection | ||
public class RedisResource { | ||
|
||
@Inject | ||
RedisService redisService; | ||
|
||
@Path("sendMessage") | ||
@POST | ||
public Response sendMessage(@QueryParam("channel") String channel, Map<String, String> message) { | ||
String newId = redisService.insert(channel, message); | ||
return Response.ok(newId).build(); | ||
} | ||
|
||
@Path("pollMessages") | ||
@GET | ||
public Response pollMessages(@QueryParam("max") Integer max, List<String> channels) { | ||
List<Map.Entry<String, List<StreamEntry>>> res = redisService.get(max, channels); | ||
return Response.ok(res).build(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
database-manipulation-tool/src/main/java/io/skodjob/dmt/service/RedisService.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,23 @@ | ||
package io.skodjob.dmt.service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.skodjob.dmt.sinkSources.RedisDataSource; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import redis.clients.jedis.resps.StreamEntry; | ||
|
||
@ApplicationScoped | ||
public class RedisService { | ||
@Inject | ||
RedisDataSource redisDataSource; | ||
|
||
public String insert(String channel, Map<String, String> message) { | ||
return redisDataSource.sendMessage(channel, message).toString(); | ||
} | ||
|
||
public List<Map.Entry<String, List<StreamEntry>>> get(int amount, List<String> channels) { | ||
return redisDataSource.pollMessages(amount, channels); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
database-manipulation-tool/src/main/java/io/skodjob/dmt/sinkSources/RedisDataSource.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,50 @@ | ||
package io.skodjob.dmt.sinkSources; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
import redis.clients.jedis.StreamEntryID; | ||
import redis.clients.jedis.params.XAddParams; | ||
import redis.clients.jedis.params.XReadParams; | ||
import redis.clients.jedis.resps.StreamEntry; | ||
|
||
@ApplicationScoped | ||
public class RedisDataSource { | ||
|
||
protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); | ||
|
||
private JedisPool pool; | ||
|
||
@Inject | ||
public RedisDataSource(@ConfigProperty(name = "data.source.redis.host") String host, | ||
@ConfigProperty(name = "data.source.redis.port") Integer port, | ||
@ConfigProperty(name = "data.source.redis.pool.max") Integer maxPool) { | ||
JedisPoolConfig config = new JedisPoolConfig(); | ||
config.setMaxTotal(maxPool); | ||
this.pool = new JedisPool(config, host, port); | ||
} | ||
|
||
public StreamEntryID sendMessage(String channel, Map<String, String> message) { | ||
Jedis jedis = pool.getResource(); | ||
return jedis.xadd(channel, message, XAddParams.xAddParams()); | ||
} | ||
|
||
public List<Map.Entry<String, List<StreamEntry>>> pollMessages(int maxAmount, List<String> channels) { | ||
|
||
Jedis jedis = pool.getResource(); | ||
HashMap<String, StreamEntryID> streams = new HashMap<>(); | ||
channels.forEach(channel -> streams.put(channel, new StreamEntryID())); | ||
|
||
return jedis.xread(XReadParams.xReadParams().count(maxAmount), streams); | ||
} | ||
|
||
} |
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