简体   繁体   English

将Java代码转换为JRuby

[英]Converting Java code to JRuby

I'm using some Java code in a JRuby project for connecting to MQ. 我在JRuby项目中使用一些Java代码来连接到MQ。 I'm new in Java and don't know how the following statements of Java can be used in JRuby. 我是Java的新手,不知道如何在JRuby中使用以下Java语句。

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 其中QueueConnection和QueueSession是在顶部导入的Java类

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

Thanks 谢谢

In Java: 在Java中:

  • Variable must declare their type. 变量必须声明其类型。 QueueConnection con = ... means the variable con is of type QueueConnection . QueueConnection con = ...表示变量conQueueConnection类型。 Types exists in Ruby too but they are not explicit, so you would simply say con = ... . 类型也存在于Ruby中,但是它们不是显式的,因此您只需说con = ...
  • Statements must be ended by semi-colons. 语句必须以分号结尾。 They are not required in Ruby. Ruby中不需要它们。

Additionally, the code you show is not quite correct, as the session variable is used in session.AUTO_ACKNOWLEDGE before it is declared. 此外,您显示的代码不是很正确,因为在声明session之前, session.AUTO_ACKNOWLEDGE使用了session变量。 AUTO_ACKNOWLEDGE is a static field of the QueueSession class, so the code should read QueueSession.AUTO_ACKNOWLEDGE . AUTO_ACKNOWLEDGEQueueSession类的静态字段,因此代码应读取QueueSession.AUTO_ACKNOWLEDGE In JRuby, static fields can be accessed using the :: syntax instead of . 在JRuby中,可以使用::语法代替来访问静态字段. .

I would therefore guess that the equivalent JRuby code of your snippet is something like this: 因此,我想您的片段的等效JRuby代码是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM