public
Last active

wrapping esper with JRuby embrace

  • Download Gist
TweetEvent.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// A simple Tweet POJO:
// $> javac TweetEvent.java
 
public class TweetEvent {
private String user;
private String text;
private String timezone;
private int retweets;
 
public TweetEvent(String user, String text, String timezone, int retweets) {
this.user = user;
this.text = text;
this.timezone = timezone;
this.retweets = retweets;
}
 
public int getRetweets() {
return retweets;
}
 
public String getText() {
return text;
}
 
public String getUser() {
return user;
}
 
public String getTimezone() {
return timezone;
}
 
public String toString() {
return user + ": " + text + " - (" + timezone + ")";
}
}
esper-jruby.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
require 'rubygems'
require 'java'
 
require '../esper-4.2.0.jar'
require '../esper/lib/commons-logging-1.1.1.jar'
require '../esper/lib/antlr-runtime-3.2.jar'
require '../esper/lib/cglib-nodep-2.2.jar'
require '../esper/lib/log4j-1.2.16.jar'
 
include_class 'com.espertech.esper.client.EPRuntime'
include_class 'com.espertech.esper.client.EPServiceProviderManager'
include_class 'com.espertech.esper.client.EPServiceProvider'
include_class 'com.espertech.esper.client.EPStatement'
 
include_class 'com.espertech.esper.client.UpdateListener'
include_class 'com.espertech.esper.client.EventBean'
include_class 'org.apache.commons.logging.Log'
include_class 'org.apache.commons.logging.LogFactory'
 
require 'tweetstream'
 
java_import 'TweetEvent'
 
class TweetListener
include UpdateListener
 
def update(newEvents, oldEvents)
newEvents.each do |event|
puts "New event: #{event.getUnderlying.toString}"
end
end
end
 
epService = EPServiceProviderManager.getDefaultProvider
statement = epService.getEPAdministrator.createEPL("select * from TweetEvent")
statement.addListener(TweetListener.new)
 
TweetStream::Client.new('username','password').track('keyword') do |t|
event = TweetEvent.new(t.user.screen_name, t.text, t.user.time_zone, t.retweet_count)
epService.getEPRuntime.sendEvent(event);
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.