Last active
May 17, 2020 21:08
-
-
Save belgoros/4ccdb2731223451c73c211de1746755f to your computer and use it in GitHub Desktop.
spring-batch to fetch PhraseApp translations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello; | |
import hello.dto.PostDto; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
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.batch.item.ItemWriter; | |
import org.springframework.batch.item.json.JacksonJsonObjectReader; | |
import org.springframework.batch.item.json.JsonItemReader; | |
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.io.UrlResource; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.StringJoiner; | |
@Configuration | |
@EnableBatchProcessing | |
public class BatchConfiguration { | |
private static final Logger log = LoggerFactory.getLogger(BatchConfiguration.class); | |
@Autowired | |
public JobBuilderFactory jobBuilderFactory; | |
@Autowired | |
public StepBuilderFactory stepBuilderFactory; | |
@Bean | |
public JsonItemReader<PostDto> itemReader() throws Exception { | |
URL url = new URL(buildUrl()); | |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
initConnection(con); | |
UrlResource phraseAppResource = new UrlResource(url); | |
int responseCode = con.getResponseCode(); | |
System.out.println("+++++++++ RESPONSE++++++++++++++ : " + responseCode); | |
final JsonItemReader<PostDto> jsonReader = new JsonItemReaderBuilder<PostDto>() | |
.name("jsonReader") | |
.resource(phraseAppResource) | |
.jsonObjectReader(new JacksonJsonObjectReader<>(PostDto.class)) | |
.strict(false) | |
.build(); | |
return jsonReader; | |
} | |
private void initConnection(HttpURLConnection con) throws IOException { | |
String apiToken = "token {phrase app token value}"; | |
con.setRequestMethod("GET"); | |
con.setRequestProperty("Content-Type", "application/json"); | |
con.setRequestProperty("Authorization", apiToken); | |
con.connect(); | |
} | |
private String buildUrl() { | |
String apiUrl = "https://classic-json-api.herokuapp.com"; | |
String postsUrl = "posts"; | |
StringJoiner joiner = new StringJoiner("/"); | |
joiner.add(apiUrl).add(postsUrl); | |
return joiner.toString(); | |
} | |
@Bean | |
public ItemWriter<PostDto> itemWriter() { | |
return items -> { | |
for (PostDto item : items) { | |
System.out.println("item = " + item); | |
} | |
}; | |
} | |
@Bean | |
public Job job() throws Exception { | |
return jobBuilderFactory.get("job") | |
.start(step()) | |
.build(); | |
} | |
@Bean | |
public Step step() throws Exception { | |
return stepBuilderFactory.get("step") | |
.<PostDto, PostDto>chunk(5) | |
.reader(itemReader()) | |
.writer(itemWriter()) | |
.build(); | |
} | |
/*@Bean | |
public JsonItemReader<TranslationDto> itemReader() throws Exception { | |
URL url = new URL(buildUrl()); | |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
initConnection(con); | |
UrlResource phraseAppResource = new UrlResource(url); | |
int responseCode = con.getResponseCode(); | |
System.out.println("+++++++++ RESPONSE++++++++++++++ : " + responseCode); | |
final JsonItemReader<TranslationDto> jsonReader = new JsonItemReaderBuilder<TranslationDto>() | |
.name("jsonReader") | |
.resource(phraseAppResource) | |
.jsonObjectReader(new JacksonJsonObjectReader<>(TranslationDto.class)) | |
.strict(false) | |
.build(); | |
return jsonReader; | |
} | |
private void initConnection(HttpURLConnection con) throws IOException { | |
String apiToken = "token {phrase app token}"; | |
con.setRequestMethod("GET"); | |
con.setRequestProperty("Content-Type", "application/json"); | |
con.setRequestProperty("Authorization", apiToken); | |
con.connect(); | |
} | |
private String buildUrl() { | |
String apiUrl = "https://api.phraseapp.com/api/v2/projects"; | |
String projectId = "{phrase app project id}"; | |
String translationsUrl = "translations"; | |
StringJoiner joiner = new StringJoiner("/"); | |
joiner.add(apiUrl).add(projectId).add(translationsUrl); | |
return joiner.toString(); | |
} | |
@Bean | |
public ItemWriter<TranslationDto> itemWriter() { | |
return items -> { | |
for (TranslationDto item : items) { | |
System.out.println("item = " + item); | |
} | |
}; | |
} | |
@Bean | |
public Job job() throws Exception { | |
log.info("+++++++++++++ importTranslationsJob +++++++++++++++"); | |
return jobBuilderFactory.get("job") | |
.start(step()) | |
.build(); | |
} | |
@Bean | |
public Step step() throws Exception { | |
log.info("+++++++++++++ step ++++++++++++++++++"); | |
return stepBuilderFactory.get("step") | |
.<TranslationDto, TranslationDto>chunk(25) | |
.reader(itemReader()) | |
.writer(itemWriter()) | |
.build(); | |
}*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello.dto; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.ToString; | |
@Getter | |
@Setter | |
@ToString | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class KeyDto { | |
private String name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello.dto; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.ToString; | |
@Getter | |
@Setter | |
@ToString | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class LocaleDto { | |
private String name; | |
private String code; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello.dto; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.ToString; | |
@Getter | |
@Setter | |
@ToString | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class PostDto { | |
private String title; | |
private String body; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hello.dto; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.ToString; | |
@Getter | |
@Setter | |
@ToString | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class TranslationDto { | |
private String content; | |
private LocaleDto locale; | |
private KeyDto key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No worries. The most important thing is to be able to help you!