Arif
Arif

Reputation: 1399

Converting Java code to JRuby

I'm using some Java code in a JRuby project for connecting to MQ. I'm new in Java and don't know how the following statements of Java can be used in JRuby.

QueueConnection con = factory.createQueueConnection();
QueueSession session = con.createQueueSession(false, session.AUTO_ACKNOWLEDGE);
session.start();

Where QueueConnection and QueueSession are Java classes that are imported on top

java_import javax.jms.QueueConnection
java_import javax.jms.QueueSession

Thanks

Upvotes: 0

Views: 148

Answers (1)

Hugo Wood
Hugo Wood

Reputation: 2260

In Java:

  • Variable must declare their type. QueueConnection con = ... means the variable con is of type QueueConnection. Types exists in Ruby too but they are not explicit, so you would simply say con = ....
  • Statements must be ended by semi-colons. They are not required in Ruby.

Additionally, the code you show is not quite correct, as the session variable is used in session.AUTO_ACKNOWLEDGE before it is declared. AUTO_ACKNOWLEDGE is a static field of the QueueSession class, so the code should read QueueSession.AUTO_ACKNOWLEDGE. In JRuby, static fields can be accessed using the :: syntax instead of ..

I would therefore guess that the equivalent JRuby code of your snippet is something like this:

con = factory.createQueueConnection()
session = con.createQueueSession(false, QueueSession::AUTO_ACKNOWLEDGE)
session.start()

Upvotes: 1

Related Questions