对于独立应用程序(对于 Spring JMS),Java main 方法应该是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2497627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What should the Java main method be for a standalone application (for Spring JMS)?
提问by Brandon
I am interested in creating a Spring standalone application that will run and wait to receive messages from an ActiveMQ queue using Spring JMS. I have searched a lot of places and cannot find a consistent way of implementing the main method for such a standalone application. There appears to be few examples of Spring standalone applications. I have looked at Tomcat, JBoss, ActiveMQ and other examples from the around the web but I have not come to a conclusion so ...
我有兴趣创建一个 Spring 独立应用程序,该应用程序将使用 Spring JMS 运行并等待从 ActiveMQ 队列接收消息。我已经搜索了很多地方,但找不到一种一致的方式来实现这种独立应用程序的主要方法。Spring 独立应用程序的示例似乎很少。我查看了 Tomcat、JBoss、ActiveMQ 和网络上的其他示例,但我还没有得出结论,所以......
What is the best practice for implementing a main method for a Java application (specifically Spring with JMS) ?
为 Java 应用程序(特别是带有 JMS 的 Spring)实现 main 方法的最佳实践是什么?
Update: Here's an example from: http://forum.springsource.org/showthread.php?t=48197Is this the best way of doing this?
更新:这里有一个例子:http: //forum.springsource.org/showthread.php?t=48197这是最好的方法吗?
public static void main(String args[]) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
. . . . .
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
回答by Droo
Attempt a read in a loop. If a message is found, process it, then sleep and iterate again. Also put some sort of terminator logic in there (interrupt the thread). You could terminate after X attempts, read from a file, kill the JVM, read a terminator msg from the queue, etc., etc.
尝试循环读取。如果找到消息,则对其进行处理,然后休眠并再次迭代。还要在其中放置某种终结器逻辑(中断线程)。您可以在 X 次尝试后终止、从文件中读取、终止 JVM、从队列中读取终止符 msg 等等。
public static void main(String[] args) {
while(true) {
// look for some terminator
// attempt to read off queue
// process message
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
break;
}
}
}
回答by Hans Westerbeek
In your main() do something like this:
在你的 main() 中做这样的事情:
// gather config files
String[] configs = { "classpath:applicationContext-myutil.xml" };
// create the app context
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(configs);
// obtain reference to your utility bean
MyUtilityBean utility = (MyUtilityBean) ctx.getBean("utility");
// invoke methods on your utility bean
utility.doSomething()
You would inject your Utiltity bean with Spring's JMS templateto do the gruntwork for your usecase.
您将使用Spring 的 JMS 模板注入您的实用程序 bean来为您的用例执行繁重的工作。
回答by soody
This is what we have, inside app-context.xml we use spring JMS classes like (org.springframework.jms.listener.DefaultMessageListenerContainerto manage number of consumers and provide custom listener using org.springframework.jms.listener.adapter.MessageListenerAdapter)
这就是我们所拥有的,在 app-context.xml 中,我们使用 Spring JMS 类(如org.springframework.jms.listener.DefaultMessageListenerContainer来管理消费者数量并使用org.springframework.jms.listener.adapter.MessageListenerAdapter提供自定义侦听器 )
app-context.xml contains all spring beans listeners and other stuff, the code below is bootstrapping Spring provided listeners on queues. So idea is to use Spring classes to manage multiple consumers. Let me know if this is what you need and need more information on configuring MessageListenerAdapter.
app-context.xml 包含所有 spring bean 监听器和其他东西,下面的代码是引导 Spring 提供的队列监听器。所以想法是使用Spring类来管理多个消费者。让我知道这是否是您所需要的,并且需要有关配置 MessageListenerAdapter 的更多信息。
public static void main(String[] args)
{
try
{
new ClassPathXmlApplicationContext("app-context.xml");
}
catch (Throwable e)
{
e.printStackTrace();
System.exit(-1);
}
}
回答by Vladimir
The main idea is to make main thread wait until app will be finished. while(!finished) is correct way to wait until main thread will be wake up.
主要思想是让主线程等待应用程序完成。while(!finished) 是等待主线程唤醒的正确方法。
finishEventHandler - is a method that handles finish/quit event.
finishEventHandler - 是一种处理完成/退出事件的方法。
I consider that JMS initialization is done in Spring conf. and in ". . . . ." section.
我认为 JMS 初始化是在 Spring conf 中完成的。并在“……”中 部分。
public static void main(String args[]) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
lock = new Object();
while(!finished) {
synchronized (lock) {
lock.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void finishEventHandler() {
finished = true;
lock.notify();
}
回答by momania
When using Spring JMS you already use components/beans in your configuration that get auto-started and will stay alive (subscribe and read from queue/topic) until you stop the application.
使用 Spring JMS 时,您已经在配置中使用了组件/bean,这些组件/bean 会自动启动并保持活动状态(从队列/主题订阅和读取),直到您停止应用程序。
To start and keep the application running, loading the applicationcontext should therefore be enough. Good practice though is to also call the registerShutdownHook, so on a application halt (i.e via ctrl+c in the console), al your beans are gracefully shutdown and disposed :)
因此,要启动并保持应用程序运行,加载应用程序上下文就足够了。好的做法是也调用 registerShutdownHook,因此在应用程序停止时(即通过控制台中的 ctrl+c),您的所有 bean 都会正常关闭和处理 :)
public static void main(String args[]) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
}