Springbatch APACHE-ACTIVEMQ Consolidation (put data into MQ, take data from MQ)

Source: Internet
Author: User


The simple test is as follows:

1: Accept the download apache-activemq-5.14.4 decompression Apache-activemq-5.14.4\bin\win64, run Activemq.bat

Start the local MQ server.

The browser allows you to view information about the local MQ server.

http://127.0.0.1:8161/admin/index.jsp

2: Put data into MQ first

The configuration is as follows:

<job id= "Jmsreadjob" >         <step id= "Jmsreadstep" >             <tasklet transaction-manager= "TransactionManager" >                 <chunk reader= "JmsItemReader"   Processor= "Creditbillprocessor"  writer= "Credititemwriter"  commit-interval= "2" ></chunk>             </tasklet>         </step> </job> 
      <!--  Read  -->     <bean:bean from a database  id= "Jmsitemreader"  scope= "Step"         class= " Org.springframework.batch.item.database.JdbcCursorItemReader " >         <bean:property name= "DataSource"  ref= "DataSource"/><bean:property name= " SQL "         value="          select biz_uuid,biz_type_code,biz_type_prop_code,biz_type_prop_name from ims_biz_args          "/>        <bean :p roperty name= "RowMapper" >             <bean:bean class= "Org.springframework.jdbc.core.BeanPropertyRowMapper" >                 <bean:property name= "Mappedclass"  value= " Com.citiccard.channel.sts.mqbatch.BizArgs "/>             </bean:bean>        </bean:property>     </bean:bean>

<!--write to MQ--
<bean:bean id= "Credititemwriter" class= "Org.springframework.batch.item.jms.JmsItemWriter" ><bean: Property Name= "Jmstemplate" ref= "Jmstemplate"/></bean:bean>

<!--MQ Object configuration--
<bean:bean id= "Jmstemplate"   class= " Org.springframework.jms.core.JmsTemplate "><bean:property name=" ConnectionFactory " ref=" Mqfactory " /><bean:property name=" Messageconverter "><bean:beanclass=" Org.springframework.jms.support.converter.SimpleMessageConverter " /></bean:property> < Bean:property name= "Defaultdestination"  ref= "Destination"  /><bean:property name= " ReceiveTimeout " value=" &NBSP;/></BEAN:BEAN> 
<bean:bean id= "Mqfactory" class= "org.apache.activemq.ActiveMQConnectionFactory" ><bean:property name= " Brokerurl "value=" tcp://localhost:61616 "/><bean:property name=" Trustallpackages "value=" true "/><bean: Property Name= "Useasyncsend" value= "true"/><bean:property name= "Redeliverypolicy" ><bean:bean class= " Org.apache.activemq.RedeliveryPolicy "><bean:property name=" maximumredeliveries "value=" 0 "/></bean: Bean></bean:property></bean:bean>
<bean:bean id= "Destination" class= "Org.apache.activemq.command.ActiveMQQueue" > <!-- MQ queue name (multiple queues can be in the MQ server)--><bean:constructor-arg value= "enrollMq444"/></bean:bean>
 <!--  processor (meaning that after you have taken the data from the source, Whether the data should be processed (e.g. I send a socket request based on this data or I multiply the value of a field by 2, etc.))--><bean:bean id= "Creditbillprocessor"  scope= "Step" class= "Com.citiccard.channel.sts.mqbatch.CreditBillProcessor" ></BEAN:BEAN> 
<!--  My example of this test means that I took the object out of the database and didn't do anything. The Credititemwriter object that sends the object directly to the Springbatch means that the Bizargs object is stored in MQ. This also means that when I fetch data from MQ, the data is a Bizargs object rather than a string of other objects. Of course I can also ToString to play, test it. -->public class creditbillprocessor implements itemprocessor<bizargs, bizargs > {private JdbcTemplate jdbcTemplate = null;public JdbcTemplate  Getjdbctemplate ()  {return jdbctemplate;} Public void setjdbctemplate (jdbctemplate jdbctemplate)  {this.jdbcTemplate =  JdbcTemplate;} @Overridepublic  bizargs process (Bizargs bizargs)  throws exception { System.out.println (Bizargs); Return bizargs;}} 
<!--Run a-->public static void main (String[]  args)  {//applicationcontext context = getcontext ("springjob/mq/ Job-db-jdbc-month01-putmq.xml "); Applicationcontext context = getcontext ("Springjob/mq/job-db-jdbc-month01-getmq.xml");// Jmstemplate jmstemplate = getjmstemplate (context);//sendmessage (jmstemplate, new  Creditbill ("4047390012345678", "Tom", 100.00, "2013-2-2 12:00:08", "lu jia zui road"));// SendMessage ("4047390012345678", "Tom", "Jmstemplate, new creditbill", "2013-2-3 10:35:21", "Lu  jia zui road ")//sendmessage (Jmstemplate, new creditbill (" 4047390012345678 "," Tom ", 360.00, "2013-2-11 11:12:38", "Longyang road")) Executejob (context,  "Jmsreadjob",new  Jobparametersbuilder (). Adddate ("Date",  new date ()));} 

After that, the APACHE-ACTIVEMQ server will have a Queue called enrollMq444 (configured in the configuration file) , and the data is consistent with the number of bars in the database.


3: Take data from MQ, not in detail, direct paste configuration

<bean:import resource= "Classpath:springjob/job-context.xml"/>

<job id= "Jmsreadjob" >         <step id= "Jmsreadstep" >             <tasklet transaction-manager= "TransactionManager" >                 <chunk reader= "JmsItemReader"   Processor= "Creditbillprocessor"  writer= "Credititemwriter"  commit-interval= "2" ></chunk>             </tasklet>         </step>    </job> 
 <bean:bean id= "Jmsitemreader" class= " Org.springframework.batch.item.jms.JmsItemReader "><bean:property name=" ItemType " value=" Com.citiccard.channel.sts.mqbatch.BizArgs "/><bean:property name=" Jmstemplate " ref=" jmstemplate "/></bean:bean>    <bean:bean id=" Jmstemplate " class=" Org.springframework.jms.core.JmsTemplate " ><bean:property name=" ConnectionFactory " ref=" Mqfactory " /><bean:property name=" Messageconverter "><bean:beanclass=" Org.springframework.jms.support.converter.SimpleMessageConverter " /></bean:property> < Bean:property name= "Defaultdestination"  ref= "Destination"  /><bean:property name= " ReceiveTimeout " value=" &NBSP;/></BEAN:BEAN> 
<bean:bean class= "Org.apache.activemq.ActiveMQConnectionFactory"  id= "Mqfactory" ><bean:property name= "Brokerurl"  value= "tcp://localhost:61616"  /> <bean:property name= "Trustallpackages"  value= "true"  /></bean:bean><bean:bean  id= "Destination2"  class= "Org.apache.activemq.command.ActiveMQQueue" ><bean:constructor-arg  value= "Enroll"  /></bean:bean><bean:bean id= "Destination"  class= " Org.apache.activemq.command.ActiveMQQueue "><bean:constructor-arg value=" enrollMq444 " />< /bean:bean> 
<bean:bean id= "creditbillprocessor"  scope= "Step" class= " Com.citiccard.channel.sts.mqbatch.GetCreditBillProcessor "></bean:bean><!--  Write database  --> <bean:bean id= "Credititemwriter" class= "Org.springframework.batch.item.database.JdbcBatchItemWriter" Scope= "Step" ><bean:property name= "DataSource"  ref= "W"  /><bean:property name= " SQL "Value=" update  ims_biz_args t  set t.biz_type_prop_name = ?  where   t.biz_uuid  = ?  " /><bean:property name=" Itempreparedstatementsetter "><bean:beanclass=" Com.citiccard.channel.sts.mqbatch.UpdateSqlSetter " / ></bean:property></bean:bean></bean:beans> 
<!--I added a haha to the name value, hahaha-->public class Updatesqlsetter implementsitempreparedstatementsetter<bizargs> {@Overridepublic void Setvalues (Bizargs Bigargs, PreparedStatement PS) throws SQLException {ps.setstring (1, Bigargs.getbiztypepropname () + "haha");p s.setstring (2, Bigargs.getbizuuid ());}}


4: Finished, try a little.


Springbatch APACHE-ACTIVEMQ Consolidation (put data into MQ, take data from MQ)

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.