简体   繁体   English

在jruby rails应用程序中延迟加载ESB

[英]Lazy loading of ESB in a jruby rails app

I have a jruby/rails app using: 我有一个使用以下命令的jruby / rails应用程序:

jruby 1.4.0
Rails 2.3.5
ActiveMQ 5.3.0
Mule ESB 2.2.1

Currently in our environment.rb file we start up Mule in the initializer. 当前,在我们的environment.rb文件中,我们在初始化程序中启动Mule。 This becomes a big pain when we go to do normal rake tasks that don't require JMS/Mule such as db:migrate as it takes a long time to startup/shutdown Mule everytime. 当我们去执行不需要JMS / Mule的常规rake任务(例如db:migrate)时,这变得很痛苦,因为每次启动/关闭Mule都花费很长时间。

The code is similar to this: 代码类似于以下内容:

APP_CONTEXT = Java::our.company.package.service_clients.Initializer.getAppContext(MULE_CONFIG_PATH)

And we use APP_CONTEXT to fetch the bean to connect to the appropriate service. 然后,我们使用APP_CONTEXT来获取bean以连接到适当的服务。

I'm trying to figure out some mechanism by which APP_CONTEXT could be lazily instantiated (not in initialize) to avoid all of the pains of having to startup Mule on initialize. 我正在尝试找出一些机制,通过该机制可以延迟实例化APP_CONTEXT(不在初始化中),以避免必须在初始化时启动Mule的所有麻烦。

Currently we have a few ruby client classes that are instantiated as a before_filter in application_controller such as @data_service = DataService.new(APP_CONTEXT) that initialize the proper java client for each request for use in our controllers. 当前,我们有一些ruby客户端类在application_controller中实例化为before_filter,例如@data_service = DataService.new(APP_CONTEXT) ,它们为在控制器中使用的每个请求初始化正确的Java客户端。

I'm open to all suggestions. 我愿意接受所有建议。 I'm having a hard time trying to find the right place to put this lazy instantiation. 我很难找到合适的位置放置此惰性实例。

In the end, (and i don't know why i didn't think of this) I've just made a class App that has a class method returning 最后,(而且我不知道为什么我没有想到这一点)我刚刚制作了一个类App,该类App返回了一个类方法

@context ||= Java::our.company.package.service_clients.Initializer.getAppContext(MULE_CONFIG_PATH)

I'm not sure what was going through my head but I thought maybe if this was referenced by two functions at the same time, I'd have two different instantiations of the AppContext, forgetting that in Ruby a class is really just a singleton object, so this will always return the one context. 我不确定发生了什么,但是我想也许如果同时被两个函数引用,我将有两个不同的AppContext实例,而忘记了在Ruby中,类实际上只是一个单例对象,因此这将始终返回一个上下文。

class App

  def self.context
    @context ||= Java::our.company.package.service_clients.Initializer.getAppContext(MULE_CONFIG_PATH)
  end
end

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

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