简体   繁体   English

使用 Spring 在 weblogic 中的服务器启动时启动线程

[英]start thread on server startup in weblogic using Spring

I want to start a thread on server startup.我想在服务器启动时启动一个线程。 My thread pulls data from db and put it in JMS queue.我的线程从数据库中提取数据并将其放入 JMS 队列中。 all the beans are defined in Spring config files.所有 bean 都在 Spring 配置文件中定义。 JMS queues and DB connection factory (CONNECTION-FACTORY)are configured on weblogic.在weblogic上配置了JMS队列和DB连接工厂(CONNECTION-FACTORY)。 I'm trying to put my thread starting code in contextInitialized method of ContextLoaderListener or init method of servlet .我试图将我的线程启动代码放在ContextLoaderListener contextInitialized方法或servlet init方法中。 However I'm getting following exception while starting server :但是我在启动服务器时遇到以下异常:

nested exception is javax.naming.NoPermissionException: User anonymous does not have permission on CONNECTION-FACTORY to perform lookup operation.嵌套异常为 javax.naming.NoPermissionException:匿名用户无权对 CONNECTION-FACTORY 执行查找操作。

My code works perfectly if i put it in doGet method of Servlet and hit the url after server startup.如果我将它放在 Servlet 的 doGet 方法中并在服务器启动后点击 url,我的代码就可以完美运行。 However I dont want to start thread manually.但是我不想手动启动线程。

I think that I'm getting this error because all the beans are not initialized properly.我认为我收到此错误是因为所有 bean 都没有正确初始化。

Where do i put my code so that thread starts automatically after server startup?我在哪里放置我的代码,以便在服务器启动后线程自动启动?

If you are creating your own thread that is most likely the problem.如果您正在创建自己的线程,这很可能是问题所在。 Inside any application server, you should let the container manage the thread pools and just schedule whatever jobs you may want to run.在任何应用程序服务器中,您应该让容器管理线程池,并安排您可能想要运行的任何作业。

Add this to your web.xml:将此添加到您的 web.xml:

<resource-ref>
    <res-ref-name>timer/MyTimer/res-ref-name>
    <res-type>commonj.timers.TimerManager</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>

In your applicationContext.xml (needs spring-context-support JAR):在您的 applicationContext.xml(需要 spring-context-support JAR)中:

<bean id="scheduler" class="org.springframework.scheduling.commonj.TimerManagerTaskScheduler" scope="singleton">
    <property name="timerManagerName" value="java:comp/env/timer/MyTimer"/>
</bean>    

Then in your contextInitialized(...) do:然后在您的 contextInitialized(...) 中执行以下操作:

scheduledFuture = scheduler.schedule(new MyJob());

And in your contextDestroyed(...) do;在你的 contextDestroyed(...) 做;

scheduledFuture.cancel(true);

In my projects I cannot find anything about configuring the timer at the application server level so I guess it "just works".在我的项目中,我找不到有关在应用程序服务器级别配置计时器的任何信息,所以我猜它“可以正常工作”。

If you want to spawn jobs asynchronously (via java.lang.concurrent.Executor interface), the procedure is similar, but you need to configure a thread pool in Weblogic:如果要异步生成作业(通过java.lang.concurrent.Executor接口),过程类似,但需要在 Weblogic 中配置一个线程池:

In web.xml;在 web.xml 中;

<resource-ref>
    <res-ref-name>wm/MyWorkManager</res-ref-name>
    <res-type>commonj.work.WorkManager</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

In applicationContext.xml:在 applicationContext.xml 中:

<bean id="executor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor" scope="singleton">
    <property name="workManagerName" value="java:comp/env/wm/MyWorkManager"/>
</bean>

In your weblogic.xml (or equivalent for EARs), something like:在您的 weblogic.xml(或 EAR 的等效文件)中,类似于:

<work-manager>
    <name>wm/MyWorkManager</name>
    <min-threads-constraint>
        <name>minThreads</name>
        <count>1</count>
    </min-threads-constraint>
    <max-threads-constraint>
        <name>maxThreads</name>
        <count>20</count>
    </max-threads-constraint>
</work-manager>    

And in your code:在你的代码中:

executor.execute(new MyRunnable());

Read Weblogic docs on timers and work managers for more job scheduling related information on this particular app server.阅读有关计时器和工作管理器的 Weblogic 文档,了解有关此特定应用程序服务器的更多作业调度相关信息。

Found solution for this one.找到了解决方案。 For exception javax.naming.NoPermissionException , we should add following lines to the weblogic.xml .对于异常javax.naming.NoPermissionException ,我们应该在weblogic.xml添加以下几行。 It initiazes webapp with user and enable JNDI lookup on deployment它使用用户初始化 webapp 并在部署时启用 JNDI 查找

<weblogic-web-app>
    <context-root>testApp</context-root>    
    <servlet-descriptor>
        <servlet-name>SchedulerServlet</servlet-name>
        <init-as-principal-name>tour_weblogic_user_name</init-as-principal-name>
    </servlet-descriptor>
</weblogic-web-app> 

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

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