Skip to content

Instantly share code, notes, and snippets.

@purplefox
purplefox / gist:1710437
Created January 31, 2012 13:20
Singleton or static
Singleton:
public class Vertx {
public static Vertx instance = new Vertx();
private Vertx() {
}
public void foo() {
}
Using JRuby 1.6.2. Running a JRuby script from Java 7, JUnit test case:
public class RubyStdErrTest extends TestCase {
public void testCatchRubyException() throws Exception {
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
String script = "raise 'Foo'";
try {
@purplefox
purplefox / gist:2168524
Created March 23, 2012 08:48
Groovy question
I have a method on a class which takes a Closure as an argument:
class Foo {
void register(Closure handler)
}
The closure represents a handler that will be called back some time in the future
In most cases users will specify the Closure directly when calling it:
I need a reference so I can unregister a handler from within a handler. otherwise would need to declare the handler first, put it in a variable and use that.
Wanna be able to do this with anon handlers
foo.registerHandler(myClosure = {
// Do something
// Now unregister handler
foo.unregisterHandler(myClosure)
})
In JavaScript I can do this: (setTimer is just a vertx function which calls the specified handler function at some time (100 ms) in the future.)
var id = vertx.setTimer(100, function() {
stdout.println("id is " + id);
});
This works because the function is a closure, and id is declared (but not initialised) before setTimer is executed.
But if I try to do the same in Groovy it complains that id is not recognised
It's nice being able to set properties by passing them to the constructor in Groovy:
def foo = new Foo(bar: 23, wibble: "hello")
But if Foo is created via a factory method instead:
def foo = Foo.createFoo(bar: 23, wibble: "hello")
It doesn't work
Ruby folks, what do you prefer:
require "vertx"
Vertx::HttpServer.new.request_handler do |req|
req.response.send_file("index.html")
end.listen(80)
or
tim@Ethel:~/wwww$ wget https://github.com/downloads/purplefox/vert.x/vert.x-1.0.beta11.tar.gz
--2012-05-04 15:02:59-- https://github.com/downloads/purplefox/vert.x/vert.x-1.0.beta11.tar.gz
Resolving github.com... 207.97.227.239
Connecting to github.com|207.97.227.239|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://cloud.github.com/downloads/purplefox/vert.x/vert.x-1.0.beta11.tar.gz [following]
--2012-05-04 15:03:00-- http://cloud.github.com/downloads/purplefox/vert.x/vert.x-1.0.beta11.tar.gz
Resolving cloud.github.com... 216.137.63.187, 216.137.63.246, 216.137.63.23, ...
Connecting to cloud.github.com|216.137.63.187|:80... connected.
HTTP request sent, awaiting response... 200 OK
Node: 1 instance
38955 Rate: count/sec: 27805.362462760677 Average rate: 27878.321139776664
41974 Rate: count/sec: 26498.840675720436 Average rate: 27779.101348453805
44974 Rate: count/sec: 28666.666666666668 Average rate: 27838.306577133455
47974 Rate: count/sec: 27333.333333333332 Average rate: 27806.728644682535
50994 Rate: count/sec: 27152.3178807947 Average rate: 27767.972702670904
53995 Rate: count/sec: 26657.780739753416 Average rate: 27706.269098990648
57014 Rate: count/sec: 28486.25372639947 Average rate: 27747.570772091065
60014 Rate: count/sec: 26666.666666666668 Average rate: 27693.53817442597
public class Client {
public static void main(String[] args) {
try {
Executor pool = Executors.newCachedThreadPool();
NioClientSocketChannelFactory channelFactory = new NioClientSocketChannelFactory(
pool, pool);