6 Temmuz 2022 Çarşamba

Jakarta EE @PostConstruct Anotasyonu

Giriş
Şu satır dahil ederiz.
import javax.annotation.PostConstruct;
Yeni kodlarda şu satır dahil ederiz.
import jakarta.annotation.PostConstruct;
Spring'e ait gibi görünse de aslında bu bir Java anotasyonu. Şöyle yaparız.
public class Foo {
  ...

  @PostConstruct
  public void fooInit(){
    ...
  }

  public Foo() {
    ...
  }
}
Açıklaması şöyle
1. When the constructor is called, the bean is not yet initialized - i.e. no dependencies are injected. In the @PostConstruct method the bean is fully initialized and you can use the dependencies.

2. this is the contract that guarantees that this method will be invoked only once in the bean lifecycle. It may happen (though unlikely) that a bean is instantiated multiple times by the container in its internal working, but it guarantees that @PostConstruct will be invoked only once.
Spring Birden Fazlan @PostConstruct Çalıştırabilir
Örnek
Şöyle yaparız
// Spring can also handle more than one method annotated with @PostConstruct, like this:
@PostConstruct
public void init() {
  log.info("1111 tests");
}

@PostConstruct
public void init2() {
  log.info("2222 tests");
}
Örnek - getter ve setter
Elimizde getter ve setter metodları olan bir sınıf olsun. Eğer constructor içinde application nesnesine erişmek istersek null pointer exception fırlatılır. Çünkü constructor metodu çalıştı ancak daha setter metodu çağrılmadı. Dolayısıyla constructor içine yazmayı düşünüdüğümüz kodu @PostConstruct olarak işaretli bir metoda taşımamız gerekir.
public class Foo {

  private String application;

  public String getApplication() {
    return application;
  }

  public void setApplication(String application) {
    this.application = application;
  }
  ...
}

5 Temmuz 2022 Salı

EJB @ConcurrencyManagement Anotasyonu

CONTAINER Managed
Açıklaması şöyle
If nothing else is specified, by default singleton session bean uses container managed concurrency. Further, if not specified, every business and timeout method have by default LockType.WRITE. Result is that there is not multiple threads concurrently executing methods in singleton and as consequence using regular java.util.HashMap is perfectly fine.
Örnek 
Şöyle yaparız
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Local;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import java.util.HashMap;
import java.util.Map;

@Singleton
@Local(MapsAndQueuesSingletonBeanLocal.class)
public class MapsAndQueuesSingletonBean implements MapsAndQueuesSingletonBeanLocal {

    private Map<String String> maps = new HashMap<>();
    
    @Override
    public String getMap(String key) {
        return maps.computeIfAbsent(key, k -> ...);
    }

}
Açıklaması şöyle
If nothing else is specified, by default singleton session bean uses container managed concurrency. Further, if not specified, every business and timeout method have by default LockType.WRITE. Result is that there is not multiple threads concurrently executing methods in singleton and as consequence using regular java.util.HashMap is perfectly fine.
BEAN Managed
Açıklaması şöyle
@Singleton in the default configuration is the perfect bottleneck. Only one thread a time can access a @Singleton instance.
The annotation @ConcurrencyManagement(ConcurrencyManagementType.BEAN) deactivates this limitation and makes a Singleton instance accessible from multiple threads:
Örnek 
Şöyle yaparız
import javax.ejb.Singleton;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import java.util.concurrent.ConcurrentHashMap;
import javax.ejb.ConcurrencyManagementType;

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class Hits {

  private ConcurrentHashMap hits = null;
	
  @PostConstruct
  public void initialize() {
    this.hits = new ConcurrentHashMap();
  }
  //cache accessors omitted
}


Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...