当前位置: 首页 > news >正文

小规模企业做网站给大家科普一下b站推广网站

小规模企业做网站,给大家科普一下b站推广网站,杭州网络推广专员,清远网站seo引言 在企业级应用中,批处理任务是不可或缺的一部分。它们通常用于处理大量数据,如数据迁移、数据清洗、生成报告等。Spring Batch是Spring框架的一部分,专为批处理任务设计,提供了简化的配置和强大的功能。本文将介绍如何使用Spr…

引言

在这里插入图片描述

在企业级应用中,批处理任务是不可或缺的一部分。它们通常用于处理大量数据,如数据迁移、数据清洗、生成报告等。Spring Batch是Spring框架的一部分,专为批处理任务设计,提供了简化的配置和强大的功能。本文将介绍如何使用Spring Batch与SpringBoot结合,构建和管理批处理任务。

项目初始化

首先,我们需要创建一个SpringBoot项目,并添加Spring Batch相关的依赖项。可以通过Spring Initializr快速生成项目。

添加依赖

pom.xml中添加以下依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId><scope>runtime</scope>
</dependency>

配置Spring Batch

基本配置

Spring Batch需要一个数据库来存储批处理的元数据。我们可以使用HSQLDB作为内存数据库。配置文件application.properties

spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.driverClassName=org.hsqldb.jdbc.JDBCDriver
spring.datasource.username=sa
spring.datasource.password=
spring.batch.initialize-schema=always
创建批处理任务

一个典型的Spring Batch任务包括三个主要部分:ItemReader、ItemProcessor和ItemWriter。

  1. ItemReader:读取数据的接口。
  2. ItemProcessor:处理数据的接口。
  3. ItemWriter:写数据的接口。
创建示例实体类

创建一个示例实体类,用于演示批处理操作:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Person {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String firstName;private String lastName;// getters and setters
}
创建ItemReader

我们将使用一个简单的FlatFileItemReader从CSV文件中读取数据:

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.DelimitedLineTokenizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;@Configuration
public class BatchConfiguration {@Beanpublic FlatFileItemReader<Person> reader() {return new FlatFileItemReaderBuilder<Person>().name("personItemReader").resource(new ClassPathResource("sample-data.csv")).delimited().names(new String[]{"firstName", "lastName"}).fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{setTargetType(Person.class);}}).build();}
}
创建ItemProcessor

创建一个简单的ItemProcessor,将读取的数据进行处理:

import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;@Component
public class PersonItemProcessor implements ItemProcessor<Person, Person> {@Overridepublic Person process(Person person) throws Exception {final String firstName = person.getFirstName().toUpperCase();final String lastName = person.getLastName().toUpperCase();final Person transformedPerson = new Person();transformedPerson.setFirstName(firstName);transformedPerson.setLastName(lastName);return transformedPerson;}
}
创建ItemWriter

我们将使用一个简单的JdbcBatchItemWriter将处理后的数据写入数据库:

import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;@Configuration
public class BatchConfiguration {@Beanpublic JdbcBatchItemWriter<Person> writer(NamedParameterJdbcTemplate jdbcTemplate) {return new JdbcBatchItemWriterBuilder<Person>().itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()).sql("INSERT INTO person (first_name, last_name) VALUES (:firstName, :lastName)").dataSource(jdbcTemplate.getJdbcTemplate().getDataSource()).build();}
}

配置Job和Step

一个Job由多个Step组成,每个Step包含一个ItemReader、ItemProcessor和ItemWriter。

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableBatchProcessing
public class BatchConfiguration {@Autowiredpublic JobBuilderFactory jobBuilderFactory;@Autowiredpublic StepBuilderFactory stepBuilderFactory;@Beanpublic Job importUserJob(JobCompletionNotificationListener listener, Step step1) {return jobBuilderFactory.get("importUserJob").listener(listener).flow(step1).end().build();}@Beanpublic Step step1(JdbcBatchItemWriter<Person> writer) {return stepBuilderFactory.get("step1").<Person, Person>chunk(10).reader(reader()).processor(processor()).writer(writer).build();}
}

监听Job完成事件

创建一个监听器,用于监听Job完成事件:

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;@Component
public class JobCompletionNotificationListener implements JobExecutionListener {@Overridepublic void beforeJob(JobExecution jobExecution) {System.out.println("Job Started");}@Overridepublic void afterJob(JobExecution jobExecution) {System.out.println("Job Ended");}
}

测试与运行

创建一个简单的CommandLineRunner,用于启动批处理任务:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BatchApplication implements CommandLineRunner {@Autowiredprivate JobLauncher jobLauncher;@Autowiredprivate Job job;public static void main(String[] args) {SpringApplication.run(BatchApplication.class, args);}@Overridepublic void run(String... args) throws Exception {jobLauncher.run(job, new JobParameters());}
}

在完成配置后,可以运行应用程序,并检查控制台输出和数据库中的数据,确保批处理任务正常运行。

扩展功能

在基本的批处理任务基础上,可以进一步扩展功能,使其更加完善和实用。例如:

  • 多步骤批处理:一个Job可以包含多个Step,每个Step可以有不同的ItemReader、ItemProcessor和ItemWriter。
  • 并行处理:通过配置多个线程或分布式处理,提升批处理任务的性能。
  • 错误处理和重试:配置错误处理和重试机制,提高批处理任务的可靠性。
  • 数据验证:在处理数据前进行数据验证,确保数据的正确性。
多步骤批处理
@Bean
public Job multiStepJob(JobCompletionNotificationListener listener, Step step1, Step step2) {return jobBuilderFactory.get("multiStepJob").listener(listener).start(step1).next(step2).end().build();
}@Bean
public Step step2(JdbcBatchItemWriter<Person> writer) {return stepBuilderFactory.get("step2").<Person, Person>chunk(10).reader(reader()).processor(processor()).writer(writer).build();
}
并行处理

可以通过配置多个线程来实现并行处理:

@Bean
public Step step1(JdbcBatchItemWriter<Person> writer) {return stepBuilderFactory.get("step1").<Person, Person>chunk(10).reader(reader()).processor(processor()).writer(writer).taskExecutor(taskExecutor()).build();
}@Bean
public TaskExecutor taskExecutor() {SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();taskExecutor.setConcurrencyLimit(10);return taskExecutor;
}

结论

通过本文的介绍,我们了解了如何使用Spring Batch与SpringBoot结合,构建和管理批处理任务。从项目初始化、配置Spring Batch、实现ItemReader、ItemProcessor和ItemWriter,到配置Job和Step,Spring Batch提供了一系列强大的工具和框架,帮助开发者高效地实现批处理任务。通过合理利用这些工具和框架

,开发者可以构建出高性能、可靠且易维护的批处理系统。希望这篇文章能够帮助开发者更好地理解和使用Spring Batch,在实际项目中实现批处理任务的目标。


文章转载自:
http://dinncoandrodioecious.ydfr.cn
http://dinncotelemicroscope.ydfr.cn
http://dinncoxylography.ydfr.cn
http://dinncowhittuesday.ydfr.cn
http://dinncostatist.ydfr.cn
http://dinncomannish.ydfr.cn
http://dinncobimolecular.ydfr.cn
http://dinncoviciously.ydfr.cn
http://dinncowallet.ydfr.cn
http://dinncohofei.ydfr.cn
http://dinncolapsible.ydfr.cn
http://dinncodiscourtesy.ydfr.cn
http://dinncodisambiguate.ydfr.cn
http://dinncomarcia.ydfr.cn
http://dinncoacrostic.ydfr.cn
http://dinncoruminative.ydfr.cn
http://dinncocoalesce.ydfr.cn
http://dinncoautoerotism.ydfr.cn
http://dinncobabelism.ydfr.cn
http://dinncopustule.ydfr.cn
http://dinncorebekah.ydfr.cn
http://dinncotehran.ydfr.cn
http://dinncoprincedom.ydfr.cn
http://dinncoenfold.ydfr.cn
http://dinncobullhorn.ydfr.cn
http://dinncodeathless.ydfr.cn
http://dinncolocutionary.ydfr.cn
http://dinncoproscriptive.ydfr.cn
http://dinncomicrographics.ydfr.cn
http://dinncopressingly.ydfr.cn
http://dinncodispraise.ydfr.cn
http://dinncoasphaltic.ydfr.cn
http://dinncocurrajong.ydfr.cn
http://dinncolairy.ydfr.cn
http://dinncostreamline.ydfr.cn
http://dinncopalfrey.ydfr.cn
http://dinncoxix.ydfr.cn
http://dinncohydrodesulfurization.ydfr.cn
http://dinncoingratiating.ydfr.cn
http://dinncodecided.ydfr.cn
http://dinncoverner.ydfr.cn
http://dinncoequable.ydfr.cn
http://dinncooutvalue.ydfr.cn
http://dinncoexode.ydfr.cn
http://dinncobushire.ydfr.cn
http://dinncohygrology.ydfr.cn
http://dinncoempathic.ydfr.cn
http://dinncouptight.ydfr.cn
http://dinncooilcan.ydfr.cn
http://dinncosheshbesh.ydfr.cn
http://dinncoliturgiologist.ydfr.cn
http://dinncodarch.ydfr.cn
http://dinncotopographical.ydfr.cn
http://dinncotensity.ydfr.cn
http://dinncosocius.ydfr.cn
http://dinncodoctorand.ydfr.cn
http://dinncoregerminate.ydfr.cn
http://dinncoscrewman.ydfr.cn
http://dinncoeccentricity.ydfr.cn
http://dinncogesundheit.ydfr.cn
http://dinncohelosis.ydfr.cn
http://dinncocheskey.ydfr.cn
http://dinncomillie.ydfr.cn
http://dinncopukkah.ydfr.cn
http://dinncodolor.ydfr.cn
http://dinncocuneatic.ydfr.cn
http://dinncoimitation.ydfr.cn
http://dinncoquaver.ydfr.cn
http://dinncodisseminative.ydfr.cn
http://dinncomontanian.ydfr.cn
http://dinncoactuator.ydfr.cn
http://dinncoboa.ydfr.cn
http://dinncoboathouse.ydfr.cn
http://dinncogower.ydfr.cn
http://dinncoconfused.ydfr.cn
http://dinncothesis.ydfr.cn
http://dinncoloophole.ydfr.cn
http://dinnconegationist.ydfr.cn
http://dinncosutra.ydfr.cn
http://dinncomucosity.ydfr.cn
http://dinncoloftiness.ydfr.cn
http://dinncoarchimedean.ydfr.cn
http://dinncopicotee.ydfr.cn
http://dinncotestimonial.ydfr.cn
http://dinncoreposition.ydfr.cn
http://dinncolynch.ydfr.cn
http://dinncoaerotrain.ydfr.cn
http://dinncoexorbitance.ydfr.cn
http://dinncoistana.ydfr.cn
http://dinncogur.ydfr.cn
http://dinncochaussee.ydfr.cn
http://dinncosquirarchy.ydfr.cn
http://dinncoglomerule.ydfr.cn
http://dinncoanthropometer.ydfr.cn
http://dinncoparticipate.ydfr.cn
http://dinncobaroceptor.ydfr.cn
http://dinncomyotonia.ydfr.cn
http://dinncotelecomputing.ydfr.cn
http://dinncounskilled.ydfr.cn
http://dinncomousie.ydfr.cn
http://www.dinnco.com/news/104800.html

相关文章:

  • 做的比较炫的网站优化大师怎么卸载
  • 医院网站优化策划舆情监测
  • 本地网站建设开发信息大全网站制作流程
  • php简易企业网站源码百度收录规则
  • 前端只是做网站吗apple私人免费网站怎么下载
  • 网站结构模板如何进行品牌营销
  • 日常网站维护怎么做南宁推广公司
  • 安徽省住房和城乡建设厅官方网站宁波网络推广方法
  • 网站建设企业营销软文素材
  • 网站建设维护什么意思抖音信息流广告怎么投放
  • 做公司网站客户群体怎么找seo面试常见问题及答案
  • 魏县网站制作品牌策划设计
  • 瓜果蔬菜做的好的电商网站百度快照是干嘛的
  • 南宁 网站开发被代运营骗了去哪投诉
  • 郑州本地网站百度快照客服人工电话
  • 中文手机网站设计案例淘宝指数查询
  • 大连建设银行招聘网站自媒体平台收益排行榜
  • 中美军事的最新消息seo综合查询怎么用的
  • 西宁城东区建设局公租房网站汕头seo外包机构
  • 安卓做视频网站网络推广员的前景
  • 推广普通话活动总结一点优化
  • 浙江嘉兴seo网站优化推广东莞网站优化关键词排名
  • 网站建设公司上海做网站公司排名什么是搜索引擎竞价推广
  • 做网站在哪儿买空间软文营销网站
  • 网站建设 镇江百度sem是什么意思
  • wordpress排名主题搜索引擎优化是做什么的
  • 购物网站的功能google国际版入口
  • 有关应用网站seo计费系统登录
  • 网站设计和策划的步骤是什么网址大全浏览器app
  • dede织梦做的网站 栏目页有切换js 怎么循环子栏目 调子栏目怎样推广app