Trying out Redis with Java

I decided to take Redis for a spin on Sunday.

Redis is a key-value store and much more. For some good Redis use-cases read:
http://highscalability.com/blog/2011/7/6/11-common-web-use-cases-solved-in-redis.html

I installed redis using macports:
sudo port install redis
Although you can install it any way you want. Check the Redis download page.

Started the server
redis-server

Coded some Java based tests using Jedis.

Output on my 2010 macbook pro when running the tests within an IDE:

main: Starting cache writing test: 100 concurrent clients doing 1000 writes each. (100000 total writes)
main: Waiting for clients to finish writes.
main: 100 clients did 1000 writes in 5181 ms.
main: Served about 19301.29 writes per second.
============================================================================
main: Publishing 1000 messages to 10 subscribers.
main: Waiting for subscribers to receive all messages.
main: Published 1000 messages and confirmed receival in 10 subscribers in 360 ms.
main: Throughput (to confirmed delivery) with 10 subscribers was about 2777.78 published messages per second.
main: 27777.78 messages received in total per second.
============================================================================
main: 100 clients doing 1000 lookups each.
main: Waiting for clients to finish lookups.
main: 100 clients did 1000 lookups each for a single cached value in 3812 ms.
main: Served about 26232.95 lookups per second.
============================================================================
main: 100 clients incrementing the same key 1000 times.
main: Waiting for threads to finish.
main: 100 concurrent clients incremented 'incrKey1337529005509' 1000 times each. Completed in 3868 ms.
main: So around 25853.15 increments per second
============================================================================
main: 5 queue listening clients dequeuing 50000 items from a queue.
main: Waiting for all items to be dequeued.
main: 5 clients listening to 'queueKey1337529009407' containing 50000 items emptied the queue in 1942 ms
main: Queue listeners processed about 25746.65 items per second.
QueueListener-2: dequeued a total of 9954 items
QueueListener-1: dequeued a total of 10031 items
QueueListener-5: dequeued a total of 9991 items
QueueListener-3: dequeued a total of 9973 items
QueueListener-4: dequeued a total of 10051 items
============================================================================

Clone the repository and try twiddling around with it yourself:
git clone git://github.com/kallebertell/redisperf.git

You should be able to run the tests with plain maven:
mvn test

Or browse the github repo here: https://github.com/kallebertell/redisperf

For more info about Jedis, check: https://github.com/xetorthio/jedis/wiki

Using Java 6 processors in Eclipse

I couldn’t find any complete tutorial on this and wasted a couple of hours figuring this stuff out.
So here you are, hopefully it’ll save you some time.

JDK5 introduced the APT (Annotation Processing Tool).
It was part of the sdk but the classes were part of the unofficial com.sun.* namespace, and you had to use the “apt” tool to process the source code.

JDK6 cleaned up the api and integrated this stuff it into javac itself so you didn’t need to use the separate apt tool anymore.

Apparently built for processing source code with annotations before they are compiled into classes, it can also be used for all kinds of fun like code generation and code analyzers which are IDE independent; and you don’t even need to use annotations necessarily. I think the JPA2 meta-model used in its criteria api is implemented with this.

I made one very contrived example of java6 processor usage with Eclipse. All of this is possible to integrate into a maven build but I’m leaving that out and focusing on how I got this processing to work within Eclipse.

So we’re creating a processor which will generate a new class for each class in projects compiled using this processor. Additionally we’ll create a Warning for each class which starts with a T. Yes it’s silly.

Step 1: Create the processor project

SillyProcessor.java:

@SupportedAnnotationTypes(value= {"*"})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class SillyProcessor extends AbstractProcessor { 

	private Filer filer;
	private Messager messager;

	@Override
	public void init(ProcessingEnvironment env) {
		filer = env.getFiler();
		messager = env.getMessager();
	}

	@Override
	public boolean process(Set elements, RoundEnvironment env) {

		for (Element element : env.getRootElements()) {

			if (element.getSimpleName().toString().startsWith("Silly")) {
				// We don't want generate new silly classes
				// for auto-generated silly classes
				continue;
			}

			if (element.getSimpleName().toString().startsWith("T")) {
				messager.printMessage(Kind.WARNING,
					"This class name starts with a T!",
					element);
			}

			String sillyClassName = "Silly" + element.getSimpleName();
			String sillyClassContent =
					"package silly;\n"
				+	"public class " + sillyClassName + " {\n"
				+	"	public String foobar;\n"
				+	"}";

			JavaFileObject file = null;

			try {
				file = filer.createSourceFile(
						"silly/" + sillyClassName,
						element);
				file.openWriter()
					.append(sillyClassContent)
					.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return true;
	}
}

Without creating this META-INF entry I couldn’t get the processor to register in Eclipse.

META-INF/services/javax.annotation.processing.Processor:

com.kerebus.annotation.processor.SillyProcessor

Its only contents is the name of the Processor implementation. I guess you might be able to list several processors here, although I’m not sure.

That’s it. Now export it as a jar and use that jar in other projects where you wish to use the processor.

STEP 2: Create a project which uses your processor.

In the properties for your new project go to Java Compiler -> Annotation Processing
Check the “Enable Project Specific Settings” and make sure “Enable annotation processing” is checked. I also changed the generated source directory to a name which didn’t start with a dot so it wouldn’t be hidden in the package explorer (files or directories which start with a dot are by default filtered away in eclipse).

Next off go to Java Compiler -> Annotation Processing -> Factory Path
Here you should add the jar of your processor project. You cannot use project references.
Press the “Advanced” button and you’ll be presented with a dialog which contains the processor you defined in your META-INF/services/javax.annotation.processing.Processor file. Select it and press ok.

Step 3: Build!

We’re practically done. Here’s what it looks like for me in my project:

So we get a warning for the Thing class because its class name start with a “T” and for each class in the project we get corresponding “Silly” classes generated. These are compiled and usable just like any other normal class.

For more info check out the eclipse jdt/apt docs, this bit about creating a code analyzer or the offical docs

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.