I want to start a background process in a Java EE (OC4J 10) environment. It seems wrong to just start a Thread with "new Thread" But I can't find a good way for this.
我想在Java EE(OC4J 10)环境中启动后台进程。用“新线程”启动一个线程似乎是错误的但是我找不到一个好方法。
Using a JMS queue is difficult in my special case, since my parameters for this method call are not serializable.
在我的特殊情况下,使用JMS队列很困难,因为此方法调用的参数不可序列化。
I also thought about using an onTimeout
Timer Method on a session bean but this does not allow me to pass parameters (as far as I know).
我还想过在会话bean上使用onTimeout Timer方法,但这不允许我传递参数(据我所知)。
Is there any "canon" way to handle such a task, or do I just have to revert to "new Thread" or a java.concurrent.ThreadPool
.
是否有任何“canon”方式来处理这样的任务,或者我只需要恢复为“new Thread”或java.concurrent.ThreadPool。
7 个解决方案
#1
Java EE usually attempts to removing threading from the developers concerns. (It's success at this is a completely different topic).
Java EE通常会尝试从开发人员关注点中删除线程。 (它在这方面的成功是一个完全不同的主题)。
JMS is clearly the preferred approach to handle this.
JMS显然是处理此问题的首选方法。
With most parameters, you have the option of forcing or faking serialization, even if they aren't serializable by default. Depending on the data, consider wrapping it in a serializable object that can reload the data. This will clearly depend on the parameter and application.
对于大多数参数,您可以选择强制或伪造序列化,即使它们在默认情况下不可序列化。根据数据,考虑将其包装在可重新加载数据的可序列化对象中。这显然取决于参数和应用。
#2
JMS is the Java EE way of doing this. You can start your own threads if the container lets you, but that does violate the Java EE spec (you may or may not care about this).
JMS是Java EE执行此操作的方式。如果容器允许,您可以启动自己的线程,但这确实违反了Java EE规范(您可能也可能不关心这一点)。
If you don't care about Java EE generic compliance (if you would in fact resort to threads rather than deal with JMS), the Oracle container will for sure have proprietary ways of doing this (such as the OracleAS Job Scheduler).
如果您不关心Java EE通用合规性(如果您实际上需要使用线程而不是处理JMS),那么Oracle容器肯定会有专有的方法(例如OracleAS Job Scheduler)。
#3
Don't know OCJ4 in detail but I used the Thread approach and a java.util.Timer approach to perform some task in a Tomcat based application. In Java 5+ there is an option to use one of the Executor services (Sheduled, Priority).
不详细了解OCJ4,但我使用Thread方法和java.util.Timer方法在基于Tomcat的应用程序中执行某些任务。在Java 5+中,可以选择使用Executor服务之一(Sheduled,Priority)。
I don't know about the onTimeout but you could pass parameters around in the session itself, the app context or in a static variable (discouraged would some say). But the name tells me it is invoked when the user's session times out and you want to do some cleanup.
我不知道onTimeout,但你可以在会话本身,应用程序上下文或静态变量中传递参数(有人说不鼓励)。但该名称告诉我,当用户的会话超时并且您想要进行一些清理时会调用它。
#4
Using the JMS is the right way to do it, but it's heavier weight.
使用JMS是正确的方法,但它的重量更重。
The advantage you get is that if you need multiple servers, one server or whatever, once the servers are configured, your "Threading" can now be distributed to multiple machines.
您获得的优势是,如果您需要多台服务器,一台服务器或其他服务器,一旦配置了服务器,您的“线程”现在可以分发到多台计算机。
It also means you don't want to send a message for a truly trivial amount of work or with a massive amount of data. Choose your interface points well.
这也意味着您不希望为真正无足轻重的工作量或大量数据发送消息。选择你的界面点。
#5
see here for some more info: *.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
有关更多信息,请参阅此处:*.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
I've been creating threads in a container (Tomcat, JBoss) with no problem, but they were really simple queues, and I don't rely on clustering.
我一直在容器(Tomcat,JBoss)中创建线程没有问题,但它们是非常简单的队列,我不依赖于集群。
However, EJB 3.1 will introduce asynchronous invocation that you may find useful: http://www.theserverside.com/tt/articles/article.tss?track=NL-461&ad=700869&l=EJB3-1Maturity&asrc=EM_NLN_6665442&uid=2882457
但是,EJB 3.1将引入您可能会觉得有用的异步调用:http://www.theserverside.com/tt/articles/article.tss?track = NL461&add = 700869&l = EJB3-1Maturity&asrc = EM_NLN_6665442&uid = 2882457
#6
Java EE doesn't really forbid you to create your own threads, it's the EJB spec that says "unmanaged threads" arn't allowed. The reason is that these threads are unknown to the application server and therefore the container cannot manage things like security and transactions on these threads.
Java EE并不是真的禁止您创建自己的线程,而是EJB规范说不允许“非托管线程”。原因是应用程序服务器不知道这些线程,因此容器无法管理这些线程上的安全性和事务。
Nevertheless there are lots of frameworks out there that do create their own threads. For example Quartz, Axis and Spring. Changes are your already using one of these, so it's not that bad to create your own threads as long as you're aware of the consequences. That said I agree with the others that the use of JMS or JCA is preferred over manual thread creation.
然而,有很多框架可以创建自己的线程。例如Quartz,Axis和Spring。改变是你已经使用其中一个,所以只要你意识到后果,创建自己的线程并不是那么糟糕。这就是说我同意其他人的观点,即JMS或JCA的使用优于手动创建线程。
By the way, OC4J allows you to create your own threads. However it doesn't allow JNDI lookups from these unmanaged threads. You can disable this restriction by specifying the -userThreads argument.
顺便说一句,OC4J允许您创建自己的线程。但是,它不允许来自这些非托管线程的JNDI查找。您可以通过指定-userThreads参数来禁用此限制。
#7
I come from a .NET background, and JMS seems quite heavy-weight to me. Instead, I recommend Quartz, which is a background-scheduling library for Java and JEE apps. (I used Quartz.NET in my ASP.NET MVC app with much success.)
我来自.NET背景,而JMS对我来说似乎相当沉重。相反,我推荐Quartz,它是Java和JEE应用程序的后台调度库。 (我在ASP.NET MVC应用程序中使用Quartz.NET取得了很大的成功。)
#1
Java EE usually attempts to removing threading from the developers concerns. (It's success at this is a completely different topic).
Java EE通常会尝试从开发人员关注点中删除线程。 (它在这方面的成功是一个完全不同的主题)。
JMS is clearly the preferred approach to handle this.
JMS显然是处理此问题的首选方法。
With most parameters, you have the option of forcing or faking serialization, even if they aren't serializable by default. Depending on the data, consider wrapping it in a serializable object that can reload the data. This will clearly depend on the parameter and application.
对于大多数参数,您可以选择强制或伪造序列化,即使它们在默认情况下不可序列化。根据数据,考虑将其包装在可重新加载数据的可序列化对象中。这显然取决于参数和应用。
#2
JMS is the Java EE way of doing this. You can start your own threads if the container lets you, but that does violate the Java EE spec (you may or may not care about this).
JMS是Java EE执行此操作的方式。如果容器允许,您可以启动自己的线程,但这确实违反了Java EE规范(您可能也可能不关心这一点)。
If you don't care about Java EE generic compliance (if you would in fact resort to threads rather than deal with JMS), the Oracle container will for sure have proprietary ways of doing this (such as the OracleAS Job Scheduler).
如果您不关心Java EE通用合规性(如果您实际上需要使用线程而不是处理JMS),那么Oracle容器肯定会有专有的方法(例如OracleAS Job Scheduler)。
#3
Don't know OCJ4 in detail but I used the Thread approach and a java.util.Timer approach to perform some task in a Tomcat based application. In Java 5+ there is an option to use one of the Executor services (Sheduled, Priority).
不详细了解OCJ4,但我使用Thread方法和java.util.Timer方法在基于Tomcat的应用程序中执行某些任务。在Java 5+中,可以选择使用Executor服务之一(Sheduled,Priority)。
I don't know about the onTimeout but you could pass parameters around in the session itself, the app context or in a static variable (discouraged would some say). But the name tells me it is invoked when the user's session times out and you want to do some cleanup.
我不知道onTimeout,但你可以在会话本身,应用程序上下文或静态变量中传递参数(有人说不鼓励)。但该名称告诉我,当用户的会话超时并且您想要进行一些清理时会调用它。
#4
Using the JMS is the right way to do it, but it's heavier weight.
使用JMS是正确的方法,但它的重量更重。
The advantage you get is that if you need multiple servers, one server or whatever, once the servers are configured, your "Threading" can now be distributed to multiple machines.
您获得的优势是,如果您需要多台服务器,一台服务器或其他服务器,一旦配置了服务器,您的“线程”现在可以分发到多台计算机。
It also means you don't want to send a message for a truly trivial amount of work or with a massive amount of data. Choose your interface points well.
这也意味着您不希望为真正无足轻重的工作量或大量数据发送消息。选择你的界面点。
#5
see here for some more info: *.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
有关更多信息,请参阅此处:*.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
I've been creating threads in a container (Tomcat, JBoss) with no problem, but they were really simple queues, and I don't rely on clustering.
我一直在容器(Tomcat,JBoss)中创建线程没有问题,但它们是非常简单的队列,我不依赖于集群。
However, EJB 3.1 will introduce asynchronous invocation that you may find useful: http://www.theserverside.com/tt/articles/article.tss?track=NL-461&ad=700869&l=EJB3-1Maturity&asrc=EM_NLN_6665442&uid=2882457
但是,EJB 3.1将引入您可能会觉得有用的异步调用:http://www.theserverside.com/tt/articles/article.tss?track = NL461&add = 700869&l = EJB3-1Maturity&asrc = EM_NLN_6665442&uid = 2882457
#6
Java EE doesn't really forbid you to create your own threads, it's the EJB spec that says "unmanaged threads" arn't allowed. The reason is that these threads are unknown to the application server and therefore the container cannot manage things like security and transactions on these threads.
Java EE并不是真的禁止您创建自己的线程,而是EJB规范说不允许“非托管线程”。原因是应用程序服务器不知道这些线程,因此容器无法管理这些线程上的安全性和事务。
Nevertheless there are lots of frameworks out there that do create their own threads. For example Quartz, Axis and Spring. Changes are your already using one of these, so it's not that bad to create your own threads as long as you're aware of the consequences. That said I agree with the others that the use of JMS or JCA is preferred over manual thread creation.
然而,有很多框架可以创建自己的线程。例如Quartz,Axis和Spring。改变是你已经使用其中一个,所以只要你意识到后果,创建自己的线程并不是那么糟糕。这就是说我同意其他人的观点,即JMS或JCA的使用优于手动创建线程。
By the way, OC4J allows you to create your own threads. However it doesn't allow JNDI lookups from these unmanaged threads. You can disable this restriction by specifying the -userThreads argument.
顺便说一句,OC4J允许您创建自己的线程。但是,它不允许来自这些非托管线程的JNDI查找。您可以通过指定-userThreads参数来禁用此限制。
#7
I come from a .NET background, and JMS seems quite heavy-weight to me. Instead, I recommend Quartz, which is a background-scheduling library for Java and JEE apps. (I used Quartz.NET in my ASP.NET MVC app with much success.)
我来自.NET背景,而JMS对我来说似乎相当沉重。相反,我推荐Quartz,它是Java和JEE应用程序的后台调度库。 (我在ASP.NET MVC应用程序中使用Quartz.NET取得了很大的成功。)