Spring Integration-Auto poll send SMS

Source: Internet
Author: User

Spring Integration Configuration
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:int= "Http://www.springframework.org/schema/integration"XMLNS:INT-JPA= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/INTEGRATION/JPA"XMLNS:INT-JMS= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/INTEGRATION/JMS"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/ Spring-integration-2.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/INTEGRATION/JMS http://www.springframework.org /schema/integration/jms/spring-integration-jms.xsd Http://www.springframework.org/schema/context http://www. Springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/integra tion http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/sc HEMA/INTEGRATION/JPA http://www.springframework.org/schema/integration/jpa/spring-integration-jpa-2.2.xsd ">        <Int-jpa:inbound-channel-adapterAuto-startup= "true"Entity-manager= "em"Send-timeout= "60000"Channel= "Process.channel"Expect-single-result= "true"Jpa-query= "Select Sysdate from Dual">        <Int:pollerFixed-delay= "60000" />    </Int-jpa:inbound-channel-adapter>            <Int:channelID= "Process.channel">        <Int:queuecapacity= "1"/>            </Int:channel>    <Int:chainInput-channel= "Process.channel">                    <Int-jpa:retrieving-outbound-gatewayEntity-manager= "em"Jpa-query= "Select sp from Smsmessage SP Where sp.tatus are null ORDER by Sp.requeston,sp.id"/>                            <Int:splitterref= "Process.processsplitter"Method= "Split"/>                <Int:service-activatorref= "Process.smssenderservice"Method= "Send" />                    <Int:pollerFixed-delay= "the"Receive-timeout= "-1"/>                </Int:chain>                <BeanID= "Process.smssenderservice"class= "Com.yd.core.service.SmsSenderService" />                <BeanID= "Process.processsplitter"class= "Com.yd.core.service.PaymentProcessSplitter" /></Beans>
Job Worker
ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.integration.MessageChannel;ImportOrg.springframework.integration.support.MessageBuilder; Public classJobworkerImplementsRunnable {Private Static Final intDefault_wait_time = 3000; @Override Public voidrun () { while(true) {            Try{Loggerutil.getjoblogger (). info ("Jobworker, ready to take job run request."); Jobrunnerrequest jobrequest=Jobmanagerservice.getjobmanager (). Takerequest ();  while(Jobrequest = =NULL) {Loggerutil.getjoblogger (). Warn ("Jobworker, jobrequest is null, would try to get the job Requet again.");                    Thread.Sleep (Default_wait_time); Jobrequest=Jobmanagerservice.getjobmanager (). Takerequest (); } loggerutil.getjoblogger (). info ("Jobworker, Received a job run request."); Messagechannel Channel=Findchannel (Jobrequest.getjobchannelid ()); if(Channel! =NULL) {channel.send (Messagebuilder.withpayload (Jobrequest.getjobmessagepayload ()). build ()); Loggerutil.getjoblogger (). info ("Jobworker, completed to sned message to Job channel"); }            }            Catch(Exception ex) {Loggerutil.getjoblogger (). Warn ("Jobworker, completed to sned message to Job channel"); }        }    }    Privatemessagechannel Findchannel (String jobchannelid) {ApplicationContext context=Applicationcontextprovider.getcontext (); if(Context = =NULL) {Loggerutil.getjoblogger (). Error (String.Format ("Jobworker, cannot get the application context, to startup job%s", Jobchannelid)); return NULL; } Object Channel=Context.getbean (jobchannelid); if(ChannelinstanceofMessagechannel) {            return(Messagechannel) channel; }        Else{Loggerutil.getjoblogger (). Error (String.Format ("Jobworker, cannot get the message bean, to startup job%s", Jobchannelid)); return NULL; }    }}
Jobmanagerservice
ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.LinkedBlockingQueue; Public Final classJobmanagerservice {PrivateBlockingqueue<jobrunnerrequest> Jobrequestqueue =NewLinkedblockingqueue<jobrunnerrequest>(); Private Static volatileJobmanagerservice jobmanagerinstnce; Private StaticObject Objsynclocker =NewObject (); PrivateJobmanagerservice () {}Private voidStartupworker () {NewThread (NewJobworker ()). Start (); }     Public StaticJobmanagerservice Getjobmanager () {if(Jobmanagerinstnce = =NULL) {            synchronized(objsynclocker) {if(Jobmanagerinstnce = =NULL) {jobmanagerinstnce=NewJobmanagerservice ();                Jobmanagerinstnce.startupworker (); }            }        }        returnjobmanagerinstnce; }     Public voidaddrequest (jobrunnerrequest request) {Try{jobrequestqueue.put (request); }        Catch(interruptedexception e) {Loggerutil.getjoblogger (). Warn (E.getmessage (), E); }    }     Publicjobrunnerrequest takerequest () {Try {            returnJobrequestqueue.take (); }        Catch(interruptedexception e) {Loggerutil.getjoblogger (). Warn (E.getmessage (), E); return NULL; }    }}

Applicatoncontextprovider
ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.ApplicationContextAware; Public classApplicationcontextproviderImplementsApplicationcontextaware {Private Static volatileApplicationContext CTX;  Public StaticApplicationContext GetContext () {returnCTX; }    Private Static synchronized voidSetContext (ApplicationContext applicationcontext) {CTX=ApplicationContext; } @Override Public voidSetapplicationcontext (ApplicationContext applicationcontext) {setContext (ApplicationContext); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.