13 Aralık 2017 Çarşamba

SpringContext LinkedMultiValueMap Sınıfı - Multimap

Giriş
Şu satırı dahil ederiz.
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
constructor
Şöyle yaparız.
MultiValueMap<String, Object> MulMap = new LinkedMultiValueMap<>();
Şöyle yaparız.
MultiValueMap<String, Integer> multiValueMap=new LinkedMultiValueMap<>();
add metodu
Elimizde şöyle bir kod olsun.
MultiValueMap<String, String> headers; new LinkedMultiValueMap<>();
Şöyle yaparız.
String key = ...;String value = ...;
headers.add(key, value);

12 Aralık 2017 Salı

SpringData Jdbc Jdbc BeanPropertyRowMapper Sınıfı

Giriş
Açıklaması şöyle. Yani RowMapper arayüzünü kullanmak daha iyi
To have tight integration between Java and ResultSet of JDBC, Spring JDBC has a RowMapper interface. A developer can either create a custom class or use BeanPropertyRowMapper, which reduces the boilerplate; however, the entity should have a public getter and setter; it might get an encapsulation issue besides it provides convenience rather than high performance. For best performance, consider using a custom RowMapper implementation.
Örnek
Elimizde bir model sınıfı olsun.
public class FooModel {

  private Long id;
  private String name;

  public InvModel() {
    super();
  }

}
Şöyle yaparız.
RowMapper<InvModel> MAPPER = BeanPropertyRowMapper.newInstance(FooModel.class);

5 Aralık 2017 Salı

SpringSecurity OAuth2 TokenEndpoint Sınıfı

Giriş
Açıklaması şöyle
Clients must be authenticated using a Spring Security Authentication to access this endpoint, and the client id is extracted from the authentication token. The best way to arrange this (as per the OAuth2 spec) is to use HTTP basic authentication for this endpoint with standard Spring Security support.

28 Kasım 2017 Salı

HttpSessionRequestCache Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
constructor
Şöyle yaparız.
HttpSessionRequestCache httpSessionRequestCache = new HttpSessionRequestCache();
setCreateSessionAllowed metodu
Şöyle yaparız.
httpSessionRequestCache.setCreateSessionAllowed(false);
Diğer
Kullanmak için şöyle yaparız.
public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
  public HttpSessionRequestCache getHttpSessionRequestCache() {
    HttpSessionRequestCache httpSessionRequestCache = new HttpSessionRequestCache();
    httpSessionRequestCache.setCreateSessionAllowed(false);
    return httpSessionRequestCache;
  }

  @Override
  protected void configure(final HttpSecurity http) throws Exception {
    http.requestCache().requestCache(getHttpSessionRequestCache());
}

27 Kasım 2017 Pazartesi

InheritableThreadLocal Sınıfı

Giriş
Açıklaması şöyle
This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.
Örnek
Şöyle yaparız.
@Service
class A{

  private InheritableThreadLocal<Student> student;

}

10 Kasım 2017 Cuma

SpringScheduling TaskScheduler Arayüzü - Dynamic Task Scheduling İçin Kullanılır

Giriş
Şu satırı dahil ederiz.
import org.springframework.scheduling.TaskScheduler;
Bu arayüzü gerçekleştiren sınıflar şöyle
ConcurrentTaskScheduler, 
DefaultManagedTaskScheduler, 
TimerManagerTaskScheduler

Bu sınıf scheduleXXX() şeklinde metodlar sağlar. Temel amacı bir işin gelecekte çağrılması için teslim edilmesini sağlamak. İşi çalıştıran sınıflar genellikle bir ThreadPool kullandıkları için aynı zamanda TaskExecutor arayüzünü de gerçekleştirirler.

Tanımlama
Örnek
Şöyle yaparız.
<task:scheduler id="scheduler" pool-size="10" />
<task:executor id="executor" keep-alive="3600" pool-size="100-200"
               queue-capacity="500" rejection-policy="CALLER_RUNS" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
schedule metodu - Runnable + Date
Örnek
Şöyle yaparız
taskScheduler.schedule(
  () -> ...,
  new Date(System.currentTimeMillis() + 3000)
);
schedule metodu - Runnable + Trigger
Trigger arayüzü işin bir sonraki çalışma zamanını hesaplar.
Bu metod ScheduledFuture nesnesi döner.
Örnek
Şöyle yaparız.
scheduler.schedule(runnable, new CronTrigger("0 15 9-17 * * MON-FRI"));
Örnek
Şöyle yaparız
@Service
public class TaskSchedulingService {

  @Autowired
  private TaskScheduler taskScheduler;

  Map<String, ScheduledFuture<?>> jobsMap = new HashMap<>();

  public void scheduleATask(String jobId, Runnable runnable String cronExpression) {
    ScheduledFuture<?> scheduledTask = taskScheduler.schedule(runnable, 
      new CronTrigger(cronExpression,
TimeZone.getTimeZone(TimeZone.getDefault().getID())));

    jobsMap.put(jobId, scheduledTask);
  }

  public void removeScheduledTask(String jobId) {
    ScheduledFuture<?> scheduledTask = jobsMap.get(jobId);
    if(scheduledTask != null) {
      scheduledTask.cancel(true);
      jobsMap.put(jobId, null);
    }
  }
}
scheduleWithFixedDelay metodu
Örnek
Şöyle yaparız
taskScheduler.scheduleWithFixedDelay(() -> ..., 1000);