Skip to content

Instantly share code, notes, and snippets.

@lenny
Created December 21, 2011 19:22
Show Gist options
  • Save lenny/1507301 to your computer and use it in GitHub Desktop.
Save lenny/1507301 to your computer and use it in GitHub Desktop.
ruby block interface for anonymous class
// ex. Java invocation
public void save() {
txTemplate.execute(new TxCallback() {
public Object execute(TransactionHolder tx) throws Exception {
//do stuff
return something;
}
});
}
# ex. ruby invocation
Transaction.execute { something }
class Transaction
class CallbackWrapper
include Java::org.aps.eop.hibernate.springtx.TxCallback
def initialize(&blk)
@blk = blk
end
def execute(tx)
@blk.call
end
end
def self.execute(&blk)
txTemplate = SpringAppContext.get_bean(bean_name)
txTemplate.execute(CallbackWrapper.new(&blk))
end
end
public interface TxCallback {
public Object execute(TransactionHolder txHolder) throws Exception;
}
public class TxTemplate {
....
public Object execute(TxCallback callback) throws Exception {
return callback.execute(txUtil.getTransaction());
}
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment