Compare commits

...

2 Commits

Author SHA1 Message Date
Terekhin Alexander 694bafcd98 Logging & logger properties 5 years ago
Terekhin Alexander 8a9e39536c Logger & profiles 5 years ago
  1. 2
      src/main/java/me/bearns/fias/config/ApplicationConfig.java
  2. 1
      src/main/java/me/bearns/fias/service/DownloaderImpl.java
  3. 43
      src/main/java/me/bearns/fias/service/StreamSaverImpl.java
  4. 6
      src/main/resources/application-default.properties
  5. 1
      src/main/resources/application-test.properties
  6. 12
      src/test/java/me/bearns/fias/TransactionsTest.java
  7. 2
      src/test/java/me/bearns/fias/VersionRepositoryTests.java
  8. 6
      src/test/java/me/bearns/fias/helper/TransactionalSaveHelper.java

@ -20,7 +20,7 @@ import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories("me.bearns.fias.repository")
@EnableTransactionManagement
@PropertySource(value = "application.properties", ignoreResourceNotFound = true)
//@PropertySource(value = "application-{profile}.properties", ignoreResourceNotFound = true)
class ApplicationConfig {
@Autowired

@ -35,7 +35,6 @@ public class DownloaderImpl implements Downloader {
if(feature != start) return feature;
pool.submit(() -> {
//todo
final URL conn;
final Path path;

@ -1,5 +1,6 @@
package me.bearns.fias.service;
import lombok.extern.slf4j.Slf4j;
import me.bearns.fias.exceptions.CommonException;
import me.bearns.fias.exceptions.UnmarshallingException;
import me.bearns.fias.helpers.UnmarshallerParameters;
@ -11,16 +12,21 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.util.Iterator;
import java.util.function.Predicate;
@Slf4j
@Component
public class StreamSaverImpl implements StreamSaver{
@Override
public void process(InputStream is, UnmarshallerParameters conf, Predicate filter) throws CommonException {
log.debug("Init unmarshaller");
try {
// create xml event reader for input stream
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
@ -35,6 +41,8 @@ public class StreamSaverImpl implements StreamSaver{
long countRead=0, countWrite=0, countErrors=0;
log.debug("Start reading stream");
// loop though the xml stream
while ((e = xmlEventReader.peek()) != null) {
@ -50,12 +58,16 @@ public class StreamSaverImpl implements StreamSaver{
} catch (Exception ex) {
countErrors++;
//log.error("Unmarshalling error {} in {}", ex.getMessage(), file.getName());
/*final Iterator attributes = ((StartElement) e).getAttributes();
attributes.forEachRemaining(
(a) -> log.error("Object dump: {} {}", ((Attribute) a).getName(), ((Attribute) a).getValue())
);*/
log.error("Unmarshalling error[{}] {}", countErrors, ex.getMessage());
if(log.isTraceEnabled()) {
//debug print
final Iterator attributes = ((StartElement) e).getAttributes();
attributes.forEachRemaining(
(a) -> log.error("Object dump: {} {}", ((Attribute) a).getName(), ((Attribute) a).getValue())
);
}
continue;
}
@ -67,8 +79,9 @@ public class StreamSaverImpl implements StreamSaver{
//Save to repository
conf.getRepository().save(obj);
countWrite++;
/*if(++countWrite%10000==0) {
/*if(countWrite%10000==0) {
repository.flush();
log.info("Write {} items to repository.", countWrite);
}*/
@ -77,21 +90,21 @@ public class StreamSaverImpl implements StreamSaver{
xmlEventReader.next();
}
}
log.info("SUCSESS: read={}, write={}", countRead, countWrite);
if(countErrors > 0) log.warn("Completed with errors, problems count: {}", countErrors);
} catch (XMLStreamException ex) {
//todo log
throw new UnmarshallingException(ex); //createXMLEventReader & peak
log.error("Caught XMLStreamException (createXMLEventReader or peak methods)");
throw new UnmarshallingException(ex);
} catch (JAXBException ex) {
//todo log
throw new UnmarshallingException(ex); //createUnmarshaller();
log.error("Caught JAXBException on createUnmarshaller()");
throw new UnmarshallingException(ex);
}
//write to DB from cache
conf.getRepository().flush();
/*log.info("SUCSESS: read={}, write={}", countRead, countWrite);
if(countErrors > 0) {
log.warn("Unmarshalling errors suppressed: {}", countErrors);
log.warn("Check data consistency in {}", file.getName());
}*/
log.debug("Repository flush completed");
}
}

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/databasename
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
logging.level.root=INFO

@ -3,3 +3,4 @@ spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
logging.level.root=DEBUG

@ -1,5 +1,6 @@
package me.bearns.fias;
import lombok.extern.slf4j.Slf4j;
import me.bearns.fias.domain.FiasVersion;
import me.bearns.fias.exceptions.UnmarshallingException;
import me.bearns.fias.helper.TransactionalSaveHelper;
@ -11,6 +12,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
@ -19,6 +21,8 @@ import java.util.List;
import static me.bearns.fias.helper.TransactionalSaveHelper.FIRST;
import static me.bearns.fias.helper.TransactionalSaveHelper.NEXT;
@Slf4j
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TransactionalSaveHelper.class)
@ -32,12 +36,14 @@ public class TransactionsTest {
@Before
public void before(){
log.info("Configure test components");
helper.setRepository(repository);
}
@Test
public void transactionTest(){
log.info("Run transaction test");
helper.addFirst();
try {
@ -45,22 +51,26 @@ public class TransactionsTest {
assert false;
} catch (Exception e) {
//just as plained
log.info("Caught exception, continue");
assert true;
}
final List<FiasVersion> versions = repository.findAll();
versions.stream().map(FiasVersion::getVersionId).forEach(System.out::println);
versions.stream().map(FiasVersion::toString).forEach(log::info);
assert versions.stream().mapToLong(FiasVersion::getVersionId).anyMatch(v -> FIRST == v);
log.info("find {}", FIRST);
assert versions.stream().mapToLong(FiasVersion::getVersionId).noneMatch(v -> NEXT == v);
log.info("not find {}", NEXT);
}
@After
public void clear(){
repository.deleteAll();
log.info("Clear FiasVersion repo");
}

@ -26,7 +26,9 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
@DataJpaTest
public class VersionRepositoryTests {
@Autowired

@ -1,5 +1,6 @@
package me.bearns.fias.helper;
import lombok.extern.slf4j.Slf4j;
import me.bearns.fias.domain.FiasVersion;
import me.bearns.fias.exceptions.CommonException;
import me.bearns.fias.exceptions.UnmarshallingException;
@ -7,6 +8,7 @@ import me.bearns.fias.repository.FiasVersionRepository;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@TestComponent
public class TransactionalSaveHelper {
@ -22,12 +24,12 @@ public class TransactionalSaveHelper {
@Transactional(rollbackFor = Exception.class)
public void addNext() throws CommonException {
final FiasVersion nextVersion = new FiasVersion(NEXT, null, null, null);
repository.save(nextVersion);
log.info("Saved {}",repository.save(nextVersion));
throw new UnmarshallingException("test");
}
public void addFirst() {
final FiasVersion version = new FiasVersion(FIRST, null, null, null);
repository.save(version);
log.info("Saved {}", repository.save(version));
}
}

Loading…
Cancel
Save