A long time ago I wrote a PoC to use Redis with jPOS, using Jedis as a dependency.
I wrote a JedisService that you can initialize using a simple QBean descriptor like this:
<!-- deploy/05_jedis_service.xml -->
<jedis-service class="org.jpos.jedis.JedisService" logger="Q2">
<property name="host" value="redishost" />
<property name="port" value="6379" />
<property name="timeout" value="120000" />
<property name="ping-every" value="60000" />
</jedis-service>
I also wrote a JedisLogListener, that looks like this:
<!-- deploy/00_jedis_logger.xml -->
<logger name="JEDISLOG" class="org.jpos.q2.qbean.LoggerAdaptor">
<log-listener class="org.jpos.util.JedisLogListener">
<property name="jedis-queue" value="jCardLog" />
</log-listener>
</logger>
The JedisService QBean implementation was super simple:
package org.jpos.jedis;
import org.jpos.iso.ISOUtil;
import org.jpos.q2.QBeanSupport;
import org.jpos.util.NameRegistrar;
import org.jpos.util.Profiler;
import redis.clients.jedis.Jedis;
@SuppressWarnings("unused")
public class JedisService extends QBeanSupport implements Runnable {
@Override
protected void initService() throws Exception {
NameRegistrar.register(getName(), this);
}
@Override
protected void startService() throws Exception {
if (cfg.getLong("ping-every", 0L) > 0L)
new Thread(this, getName()).start();
}
@Override
protected void stopService() throws Exception {
NameRegistrar.unregister(getName());
}
public Jedis getConnection()
{
getLog().info("getting a new jedis connection");
Jedis jedis = new Jedis(
cfg.get("host", "localhost"),
cfg.getInt("port", 6379),
cfg.getInt("timeout", 120000)) ;
return jedis;
}
public void run() {
while (running()) {
try {
Profiler prof = new Profiler();
Jedis jedis = getConnection();
jedis.ping();
prof.checkPoint("PING");
jedis.quit();
getLog().info("PING", prof);
} catch (Exception e) {
getLog().warn(e.getMessage());
}
ISOUtil.sleep(cfg.getLong("ping", 60000L));
}
}
}
as well as the JedisLogListener:
package org.jpos.util;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOUtil;
import org.jpos.jedis.JedisService;
import org.jpos.space.MD5Template;
import org.jpos.space.Space;
import org.jpos.space.SpaceFactory;
import redis.clients.jedis.Jedis;
@SuppressWarnings("unused")
public class JedisLogListener implements Configurable, LogListener, Runnable {
String jedisService;
String jedisQueue;
Space sp = SpaceFactory.getSpace();
public static final long BUFFER_TIMEOUT = 300000L;
public static final long RELAX = 1000L;
public LogEvent log(LogEvent logEvent) {
sp.out(jedisQueue, new FrozenLogEvent(logEvent), BUFFER_TIMEOUT);
return logEvent;
}
public void setConfiguration(Configuration cfg) throws ConfigurationException {
jedisService = cfg.get("jedis-service", "jedis-service");
jedisQueue = cfg.get("jedis-queue", null);
if (jedisQueue == null)
throw new ConfigurationException("jedis-queue property not configured");
if (jedisService == null)
throw new ConfigurationException("jedis-service property not configured");
new Thread(this, "JedisLogger:" + jedisService + ":" + jedisQueue).start();
}
public void run() {
for (int i=0;;) {
try {
JedisService js = (JedisService) NameRegistrar.getIfExists(jedisService);
if (js == null) {
ISOUtil.sleep(RELAX);
if (i++ > 60)
throw new NameRegistrar.NotFoundException(jedisService + " not found");
continue;
}
FrozenLogEvent evt = (FrozenLogEvent) sp.rd(jedisQueue, 5000L);
if (evt != null) {
Jedis jedis = js.getConnection();
jedis.rpush(jedisQueue, evt.toString());
sp.inp(jedisQueue);
jedis.quit();
}
} catch (Exception e) {
Log.getLog("Q2", "jedis-log-listener").error(e);
ISOUtil.sleep(RELAX); // take it easy on errors
}
}
}
}
Hope this gives you some pointers.
--
--
jPOS is licensed under AGPL - free for community usage for your open-source project. Licenses are also available for commercial usage. Please support jPOS, contact: sa...@jpos.org
---
You received this message because you are subscribed to the Google Groups "jPOS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jpos-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/f35461d3-53b6-4c6b-ae4c-627b364f26c9n%40googlegroups.com.
--