Friday, May 14, 2010

Pointers in JNI / C++

When writing native code for Java (JNI), It's a common behaviour to store the pointer as long in a field in Java, and do a type-cast conversion on the native code back to the original pointer.


I did that on the native module on HornetQ, however this seems to eventually fail.

when using different bit sets as the target compilation (32 bits for instance) this seems to fail when you move back and forth.


The best way for that would be to use a Native Byte Buffer. This way you don't need to worry about conversions. You can just use a pointer back and forth:


Example:


In your Java class, you create this signature:

public class SomeClass
{
public native ByteBuffer initPointer();
}


And in your C++ code:


JNIEXPORT jobject JNICALL Java_SomeClass_initPointer
(JNIEnv * env, jobject obj)
{
return env->NewDirectByteBuffer(myPointer, 0); // size = 0, you don't want anyone to change it
}




Later, any C++ code can get back the pointer by doing:


return (MyClass *) env->GetDirectBufferAddress(myPointer);




I know the word pointer makes this a bit sour, but maybe it will be helpful to someone some day :-)

Wednesday, September 2, 2009

HornetQ and Maven

Andy Taylor has written a nice blog on how to use HornetQ on Maven. Very nice article:


http://hornetq.blogspot.com/2009/09/hornetq-simple-example-using-maven.html

Tuesday, August 11, 2009

Best Practice on Junit: Always set your fields to null on tearDown

This was a surprise to me today. Just wanted to share it thourgh a small blog entry, so maybe others are also going through the same thing.

Say you have this test:


public class MyTest extends TestCase
{

Server server;

public void setUp()
{
server = new Server();
}

public void testOne() throws Exception
{
System.gc();
Thread.sleep(1000);
}

public void testSecond() throws Exception
{
System.gc();
Thread.sleep(1000);
}

public void testThird() throws Exception
{
System.gc();
Thread.sleep(1000);
}
}


JUnit will hold one instance of MyTest for every method being executed. So if Server is a heavy weight object you will end up with three instances in the memory, until all the test methods on this class are executed.

So, as a best practice on unit tests aways set big objects such as server, connections or anything lilke that to null on a junit test.

Monday, July 27, 2009

JBoss Messaging 2.0 beta 4 is released

We just released Messaging 2.0 beta 4, and it' s ready to download.

I' m pleased with the progress we are making, and we are excited about what we are doing.

We still have work to do but we are very confident about the quality of the work we are doing.

Look at the documentation and you will know what I' m talking about.

Friday, July 17, 2009

Fedora 11 finally got me...

I have been using a competitor distribution for very long (about 2 years) even though I work for RedHat / JBoss, just because it was easier. I couldn' t figure out how to do a few things.

I have tried to come to Fedora a few times.. and I could never feel comfortable.


But Fedora 11.. that' s really nice. I would say I don't even feel I'm on Linux. I'm feeling very comfortable like using my wife's Mac OS X :-)

The WebCam works, wireless... video resolution.. even Skype... everything smooth.

It could be also the Lenovo T500 behaves really well with Linux.. but still... Fedora 11 has got me.

I' m a Fedora user from now on :-)

Friday, June 26, 2009

Everybody Loves Lula!


I just love my president! Maybe he is also wearing a JBoss shirt?

This picture was taken at FISL

Speaking as a brazilian citizen, we had so many highly educated presidents before him, however this government as far as I know has promoted open source in a much better scale than any other previous government.

It is really nice seeing the government promoting open source there.

Friday, February 20, 2009

Performance comparisson on JBoss Remoting and Serialization

Afonso Olias just made a Nice Post regarding JBoss Remoting and Serialization performance.