24 Kasım 2022 Perşembe

Jakarta EE Concurrency ContextService Arayüzü

Giriş
Açıklaması şöyle
Used when you need an object to run on a non managed thread using a Jakarta EE Context.
Key for maintaining Security Context that can be propagated to other threads
One example is a JMX Notification Listener
Örnek
Şöyle yaparız
@Resource
ContextService contextSvc;

@GET
@Path("runnableproxy")
@Produces(MediaType.TEXT_PLAIN)
public String useProxy() {
  Runnable instance = 
    () -> System.out.println("Running in context");

  Runnable proxy = contextSvc.createContextualProxy(instance,Runnable.class);
  Thread thread = new Thread(proxy);
  thread.start();
  return "Started new thread";
}





Jakarta EE Concurrency @Schedule

Örnek
Şöyle yaparız
//Like the EJB Timer annotation but for other components e.g. CDI Beans (non-persistent)
@ApplicationScoped
public class ScheduledBean {

  @Schedule(executor="MyExecutor", minute="*/5")
  public void fiveMinuteRule() {
  }
}


Jakarta EE Concurrency @Lock

Örnek
Şöyle yaparız
//Provides locking semantics for CDI Beans – especially useful for ApplicationScoped
//Semantics would be similar to EJB Locks
@ApplicationScoped
public class CDIBean {

  @Lock(LockType.READ) 
  public void doReadMany(){}

  @Lock(LockType.WRITE) 
  public void doOneWriter() {}

}

Jakarta EE Concurrency @MaxConcurrency

Örnek
Şöyle yaparız
//Limits through Semaphore the number of threads that can execute a method or all
//methods concurrently.
@ApplicationScoped
public class CDIBean {

  @MaxConcurrency(2
  public void maxTwoThreadsMethod(){};

  @MaxConcurrency(10
  public void maxUseofAPI(){}

}

Jakarta EE Concurrency

Giriş
Şu anotasyonlar kullanılabilir

Ayrıca şu sınıflar var
ManagedThreadFactory

Ayrıca Deployable Managed Objects olarak şunlar var
ContextServiceDefinition
ManagedExecutorDefinition
ManagedScheduledExecutorDefintion
ManagedThreadFactoryDefinition

Maven
Örnek - Jakarta 8
Şu satırı dahil ederiz
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
<version>1.1.2</version>
</dependency>
Örnek - Jakarta 9
Şu satırı dahil ederiz
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
<version>2.0</version>
</dependency>
Örnek - Jakarta 10
Şu satırı dahil ederiz
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
<version>3.0.2</version>
</dependency>

8 Kasım 2022 Salı

JCache - JSR 107

Giriş
İlk olarak 2011 yılında Java EE 7 ile ortaya çıktı. Açıklaması şöyle
The JVM ecosystem has an official Cache API, known as JCache, or JSR 107. It’s a specification with an API that describes four annotations, i.e., @CacheResult, @CachePut, @CacheRemove, and @CacheRemoveAll. Vendors are to implement the specification.

The Spring framework is pretty widespread in the JVM ecosystem. It also provides a caching API. Historically, it predates JCache. While different, the API is very similar to JCache’s. Spring offers out-of-the-box integration code for a couple of caches, while a couple of others do provide Spring integration.
Java Cache Providers
Liste şöyle. Bunların içinden sadece Guava ve Geode JCache desteği sunmuyor.
  1. Java Caching System
  2. Guava
  3. Caffeine
  4. Ehcache
  5. Infinispan
  6. Coherence Community Edition
  7. Ignite
  8. Geode
  9. Hazelcast
Spring vs JCache
Anotasyonlar şöyle

JCache API interfaces
Önemli sınıflar şöyle
1. javax.cache.CacheManager
2. javax.cache.Cache 
3. javax.cache.configuration.Configuration

JCache Annotations 
@CacheResult
@CachePut
@CacheRemove

CacheManager Arayüzü
Şu satırı dahil ederiz
import javax.cache.CacheManager;
Açıklaması şöyle
It enables us to establish, configure and close Caches.
Bu arayüzü gerçekleştiren sınıflardan birisi. İlgili notlarım burada
import com.hazelcast.cache.HazelcastCacheManager;
@CacheResult
Örnek
Şöyle yaparız
public class ServiceImpl {

  @Override
  @CacheResult(cacheName = ..., cacheKeyGenerator = DefaultObjectsCacheKeyGenerator.class)
  public Foo getObject(@CacheKey String userId, @CacheKey String customerId) {
    ...
  }
}



Bean Validation @GroupSequence Anotasyonu

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