Skip to content

Instantly share code, notes, and snippets.

@ghostflare76
Created November 25, 2015 01:06
Show Gist options
  • Save ghostflare76/cc08f06aea2dd8c5cc9a to your computer and use it in GitHub Desktop.
Save ghostflare76/cc08f06aea2dd8c5cc9a to your computer and use it in GitHub Desktop.
package kr.co.plaync.lineage2.app.batch.rank.job;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import kr.co.plaync.lineage2.app.batch.powerbook.listener.CustomJobExecutionListener;
import kr.co.plaync.lineage2.opensource.jdbc.config.DbconnInfo.RcReplication;
import kr.co.plaync.lineage2.opensource.redis.repository.RedisRepository;
@Configuration
public class DeleteExpRankJobConfiguration {
private static final Integer BATCH_SIZE = 1000;
public enum SERVERINFO {
LINEAGE2_REPLICATION101("CLASSIC","101"),
LINEAGE2_REPLICATION102("CLASSIC","102"),
LINEAGE2_REPLICATION104("CLASSIC","104"),
LINEAGE2_REPLICATION107("CLASSIC","107");
private String serverType;
private String serverId;
SERVERINFO(String serverType, String serverId) {
this.serverType = serverType;
this.serverId = serverId;
}
public String getServerType() {
return serverType;
}
public String getServerId() {
return serverId;
}
}
@Autowired
private CustomJobExecutionListener customJobExecutionListener;
@Autowired
private StepBuilderFactory stepBuilders;
@Autowired
private JobBuilderFactory jobBuilders;
@Autowired
@Qualifier(RcReplication.LINEAGE2_GAME_INFO)
private DataSource dataSource;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
RedisRepository redisRepository;
@Bean(name = "deleteIngameRankJob")
public Job job() {
return jobBuilders.get("deleteIngameRankJob")
.listener(customJobExecutionListener)
.start(step())
.build();
}
@Bean(name = "deleteIngameRankStep")
public Step step() {
return stepBuilders.get("deleteIngameRankStep")
.tasklet(tasklet())
.transactionAttribute(transactionAttr())
.build();
}
@Bean
public TransactionAttribute transactionAttr() {
RuleBasedTransactionAttribute tr = new RuleBasedTransactionAttribute();
tr.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
tr.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
return tr;
}
@Bean(name = "deleteIngameRankTask")
protected Tasklet tasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
int deleteCount = 0;
for (final SERVERINFO serverInfo : SERVERINFO.values()) {
StringBuffer deleteSql = new StringBuffer();
deleteSql.append(" WHILE (1>0) ");
deleteSql.append(" BEGIN");
deleteSql.append(" DELETE TOP (").append(BATCH_SIZE).append(")");
deleteSql.append(" FROM GrowRanking");
deleteSql.append(" WHERE insertDate = CONVERT(CHAR(10), getdate()-1, 112) ");
deleteSql.append(" AND serverId = ").append(serverInfo.serverId);
deleteSql.append(" IF @@ROWCOUNT = 0 BREAK");
deleteSql.append(" END");
jdbcTemplate.execute(deleteSql.toString());
}
contribution.incrementWriteCount(deleteCount);
return RepeatStatus.FINISHED;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment