Ruby vs Java (this,self/main)

First, thank you for any help you can provide.

The issue:

The jruby version of this java app will not output callbacks in the
running
thread…or to the running command line process. The java version works,
but
the ruby version goes straight to the command line and waits for more
user
input instead of waiting and outputting server responses. The code will
probably explain it better:

Java version

public class FeedSubscriberPublisher implements PFSubscriber {
DFConn _dfConn; // Connection to the establishment server
(authentication etc)
PFConn _pfConn; // Connection to the feed event server
public FeedSubscriberPublisher(DFConn df) {
_dfConn = df;
_pfConn = FSStarter.startConn(_dfConn, this);
_pfConn.start();
System.out.println(“Subscribing to event PFEventFeedUpdate…”);
_psConn.subscribe(“PFEventFeedUpdate”);
}

public void newEvent(PFEvent event) {
        System.out.println("Feed Details: " + event);
}

public static void main(String args[]) {
    DFConn df = null;
    df = Establisher.connect(args, "FeedSubscriberPublisher");
    FeedSubscriberPublisher sub = new FeedSubscriberPublisher(df);
}

}

Ruby Version:

df = Java::Establisher.connect(user_pass_etc, “FeedSubscriberPublisher”)
pfconn = Java::FSStarter.startConn(df, self)
pfconn.start
puts “Subscribing to Feed events”
pfconn.subscribe(“PFEventFeedUpdate”)
def newEvent(event)
puts event
end

The Ruby version breaks when providing ‘self’ as the ‘this’ equivalent
when
connecting(line two in the ruby script, here it is in IRB):

irb(main):007:0> pfconn = Java:: FSStarter.startConn(df, self)
NameError: no method ‘startConn’ with arguments matching [
com.feed.service.DFConn, org.jruby.RubyObject]
from
/Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:579:in
matching_method' from /Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:126:instartConn’
from
/Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:85:in
startConn' from (irb):1:inbinding’

And when I don’t provide ‘self’ at all, it throws no errors however it
never
seems to wait for client input. I.e:

irb(main):009:0> df = Java::Establisher.connect(user_pass_etc,
“FeedSubscriberPublisher”)
irb(main):010:0> pfconn = Java::FSStarter.startConn(df)
=> #<#Class:01x1f38c53:0x2cd1fc6 @java_object=
com.feed.service.DFConn@73cb02>
irb(main):011:0> pfconn.start
=> nil
irb(main):012:0> pfconn.subscribe(“PFEventFeedUpdate”)
=> nil
irb(main):013:0> def newEvent(event)
irb(main):014:1> puts event.toString
irb(main):015:1> end
=> nil
irb(main):016:0> puts “Why can I type here? I want this thing to stop
accepting input and start ouputting callbacks from the server!”
Why can I type here? I want this thing to stop accepting input and start
ouputting callbacks from the server!
=> nil
irb(main):017:0>

I’m not very familiar with java as you can probably tell. What gives?
Surely
it’s a simple fix I’m not grasping.

Thanks for any help or insight you could provide.

list. rb wrote:

The Ruby version breaks when providing ‘self’ as the ‘this’ equivalent when
connecting(line two in the ruby script, here it is in IRB):

I think the problem here is that the “self” you’re passing doesn’t
implement whatever interfaces you need on the Java side.

Because we have to match the static types Java’s looking for, simply
implementing the method isn’t enough; you need to give JRuby a chance to
actually create a real Java type:

class MySubscriber
include Java::PFSubscriber

def newEvent(event)
puts event
end
end
df = Java::Establisher.connect(user_pass_etc, “FeedSubscriberPublisher”)
pfconn = Java::FSStarter.startConn(df, MySubscriber.new)
pfconn.start
puts “Subscribing to Feed events”
pfconn.subscribe(“PFEventFeedUpdate”)

There’s also some shortcut syntaxes that are somewhat experimental, but
make this process a little easier:

pfconn = Java::FSStarter.startConn(df, PFSubscriber.impl {|method,
*args| puts args[0]})

  • Charlie

Awesome Charles.

I had to make one change to your suggestion:
class MySubscriber < com.feed.service.PFSubscriber
def newEvent(event)
puts event
end
end

For some reason, the other syntax was throwing an error.

Thanks a million for the help

list. rb wrote:

Awesome Charles.

I had to make one change to your suggestion:
class MySubscriber < com.feed.service.PFSubscriber
def newEvent(event)
puts event
end
end

For some reason, the other syntax was throwing an error.

If PFSubscriber is an interface, the above syntax doesn’t work in 1.0.
What version of JRuby are you using?

If it’s not an interface, nevermind :slight_smile:

  • Charlie

java-jruby0.9.8

The error I get is in javasupport:551 method_missing: undefined method
java_class for NilClass (NoMethodError)

in StartConnection…

It sounds like I should upgrade then?

list. rb wrote:

java-jruby0.9.8

The error I get is in javasupport:551 method_missing: undefined method
java_class for NilClass (NoMethodError)

in StartConnection…

It sounds like I should upgrade then?

Yes, I’d recommend upgrading. You don’t want to be building apps on
pre-1.0 due to the syntax changes that happened. They’re not major, but
it would be unfortunate to have to go back and modify things.

  • Charlie

And yes, it’s a public interface :slight_smile: