22 Ağustos 2017 Salı

SpringBatch JdbcCursorItemReader Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcCursorItemReader;
constructor
Şöyle yaparız.
JdbcCursorItemReader<Foo> databaseReader = new JdbcCursorItemReader<>();
setDataSource metodu
Şöyle yaparız.
DataSource dataSource = ...;
databaseReader.setDataSource(dataSource);
setRowMapper metodu
Şu satırı dahil ederiz.
import org.springframework.jdbc.core.BeanPropertyRowMapper;
Şöyle yaparız.
databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Foo.class));
setSql metodu
Örnek
Şöyle yaparız.
private static final String QUERY_FIND_STUDENTS =
        "SELECT " +
            "email_address, " +
            "name, " +
            "purchased_package " +
        "FROM STUDENTS " +
        "ORDER BY email_address ASC";

databaseReader.setSql(QUERY_FIND_STUDENTS);
Örnek
Şöyle yaparız.
@Autowired
public DataSource dataSource;


@Bean
public JdbcCursorItemReader<User> reader() {
  JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
  reader.setDataSource(dataSource);
  reader.setSql("SELECT id,name FROM employee");
  reader.setRowMapper(new UserRowMapper());
  return reader;
}

public class UserRowMapper implements RowMapper<User> {

  @Override
  public User mapRow(ResultSet rs, int rowNum) throws SQLException {
    User user = new User();
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));

    return user;
  }

}

8 Ağustos 2017 Salı

SpringMVC RequestContextHolder Sınıfı

Giriş
Spring MVC projesinde kullanılır. Açıklaması şöyle
This will be populated by the standard spring mvc dispatch servlet.
currentRequestAttributes metodu
ServletRequestAttributes nesnesi döner. 
Örnek
Şöyle yaparız.
ServletRequestAttributes attr = (ServletRequestAttributes)
  RequestContextHolder.currentRequestAttributes();
getRequestAttributes metodu
Örnek
Şöyle yaparız
@Component
public class RequestUtils {

  private final ObjectMapper objectMapper;

  private static final String APP_CONTEXT_HEADER = "X-App-Context";
  private static final String REQUEST_ATTRIBUTE_APP_CONTEXT = "appContext";

  public RequestUtils(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }

  public void storeContextInformationAsRequestAttribute(HttpServletRequest request)
throws IOException {
    String jsonHeader = request.getHeader(APP_CONTEXT_HEADER);

    if (jsonHeader != null) {
      request.setAttribute(REQUEST_ATTRIBUTE_APP_CONTEXT,
        objectMapper.readValue(jsonHeader, ContextInformation.class));
      }
  }

  public Optional<ContextInformation> getContextInformation() {
  return getAttributeFromRequest(REQUEST_ATTRIBUTE_APP_CONTEXT, ContextInformation.class);
  }

  private <T> Optional<T> getAttributeFromRequest(String attributeName, Class<T> type) {
    Object attribute = RequestContextHolder.getRequestAttributes()
.getAttribute(attributeName, 0);
    if (attribute != null && type.isAssignableFrom(attribute.getClass())) {
      return Optional.of((T) attribute);
    }
    return Optional.empty();
  }
}