27 Ağustos 2020 Perşembe

@FunctionalInterface Anotasyonu

Giriş
Bir arayüzün Object sınıfından override etmediği, static ve default metodlar hariç tek bir metod tanımlamasını sağlar. Arayüz generic parametreler alabilir veya almayabilir. Açıklaması şöyle
A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.
Bir sınıfın static metodu, FunctionalInterface'e cast edilebilir.

Örnek
Şöyle yaparız. Burada Object'ten gelen equals override edilse bile @FunctionalInterface tanımını bozmaz.
@FunctionalInterface
public interface Comparator<T> {

  // abstract method
  int compare(T o1, T o2);

  //overriding public methods of `java.lang.Object`, so it does not count
  boolean equals(Object obj);

}
Örnek
Aşağıdaki kod iki metod tanımlandığı için hata verir.
@FunctionalInterface
public interface Foo{
  public void doSomething();
  public void doSomethingElse();
}
Çıktı olarak şunu alırız.
Invalid '@FunctionalInterface' annotation; Foo is not a functional interface
Örnek
Şöyle yaparız.
@FunctionalInterface 
interface Rideable {
  Car getCar (String name);
}
Örnek - Tek Generic Parametre
Şöyle yaparız.
@FunctionalInterface
private interface Myonsumer<A> {
    void accept(A a);
}
Örnek - 3 Generic Parametre
3 parametre alıp Foo dönen arayüz için şöyle yaparız.
@FunctionalInterface
interface Function<LeadMaster,JSONObject,String,Foo>
{
    public Foo apply(LeadMaster lead,JSONObject jsonObject,String str);
}
Örnek
Exception atan metod tanımlamak için şöyle yaparız.
@FunctionalInterface
public interface Factory<R, T, X extends Throwable> {
    public R newInstanceFor(T t) throws X;
}

23 Ağustos 2020 Pazar

jcmd komutu - Çalışmakta Olan Uygulamaya Diagnostic Command Requests Gönderir

Giriş
Açıklaması şöyle
As per the manual, jcmd is a utility which sends diagnostic command requests to a running Java Virtual Machine i.e. JVM.
This utility gets various runtime information from a jvm. And, thing to note is, it must be used on the same machine on which jvm is running.
Bu komutun çıktısı şöyle. İlk sütun PID numarası, ikinci sütun uygulamanın ana sınıfı
$ jcmd
15308 jdk.jcmd/sun.tools.jcmd.JCmd
1836 se.hirt.jmc.tutorial.donothing.DoNothing
Örnek
Bir java uygulamasının ana sınıfını görmek için şöyle yaparız. find /i ile case sensitive olmadığını belirtiriz. find /n ile satır numarasını alırız.
jcmd | find /I /N "sun.tools.jconsole.JConsole"


IF "%ERRORLEVEL%" GTR "0" (
    jconsole.exe
)

IF %ERRORLEVEL% EQU 0 (
    echo Programm is running 
) 
FJR.start seçeneği
"Flight Recorder" aracını başlatmak için kullanılır. PID numarası 1836 olan uygulamak için şöyle yaparız
jcmd 1836 FJR.start duration=33s filename=start.recorder.fjr
Bu dosya "Mission Control" ile görüntülenebilir.

GC.class_histogram seçeneği
Metaspace bilgisini yazar
Örnek
Şöyle yaparız
# Ekrana
jcmd {pid} GC.class_histogram

# Dosyaya
jcmd {pid} GC.class_histogram filename={file-path}

GC.heap_dump seçeneği
Çalışmakta olan uygulamanın heap dump çıktısını oluşturur

Örnek
Şöyle yaparız
jcmd 37320 GC.heap_dump /opt/tmp/heapdump.bin
4. Thread.print seçeneği

5. VM seçenekleri
jcmd komutu VM Seçenekleri yazısına taşıdım


Off-Heap Seçenekleri
Off-Heap yerine bazen "Direct Memory" de deniliyor. Uygulamayı bazı seçenekler ile çalıştırmış olmak gerekir. Şöyle yaparız
-XX:NativeMemoryTracking=detail-XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics

19 Ağustos 2020 Çarşamba

JMS MessageConsumer Arayüzü

Giriş
Destination olarak Queue, TemporaryQueue, TemporaryTopic, Topic kullanılır. JMS 1.1 ile kullanılır.

constructor 
Örnek - Destination
Queue yoksa hem Queue yaratmak hem de okumak için şöyle yaparız.
Destination destination = ...;
Session session = ...;
MessageConsumer messageConsumer = session.createConsumer(destination);
Örnek - Queue
Şöyle yaparız
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
Örnek - Topic
Şöyle yaparız.
private void createUnsharedConsumer(ConnectionFactory connectionFactory, Topic topic)
  throws JMSException {

   Connection connection = connectionFactory.createConnection();

   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   MessageConsumer messageConsumer = session.createConsumer(topic);

   connection.start();

   Message message = messageConsumer.receive(10000);

   while (message != null) {
      System.out.println("Message received: " + ((TextMessage) message).getText());
      message = messageConsumer.receive(10000);
   }
   connection.close();
}
receive metodu
Örnek
Şöyle yaparız
ConnectionFactory connectionFactory = ...
String QUEUE_NAME = "user_creation_queue";

void pollUserFromQueue() throws Exception {
  Session session = ...;
  Destination destination = session.createQueue(QUEUE_NAME);
  MessageConsumer consumer = session.createConsumer(destination);
  Message message = consumer.receive();

  ObjectMessage objMessage = (ObjectMessage) message;
  User user = (User) objMessage.getObject();
  ...  
}
receive metodu - timeout
Örnek
Şöyle yaparız
Message message = consumer.receive(120000);
if (message instanceof TextMessage) {
  TextMessage textMessage = (TextMessage) message;
  text = textMessage.getText();
  System.out.println("MESSAGE RECEIVED " + System.currentTimeMillis());
}
Örnek
Elimizde şöyle bir kod olsun.
class MyConsumer extends Thread {
  private final Connection connection;
  private final Destination destination;

  MyConsumer(Connection connection, Destination destination) {
    this.connection = connection;
    this.destination = destination;
  }

  @Override
  public void run() {
    try (Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE)) {
      MessageConsumer messageConsumer = session.createConsumer(destination);
      connection.start();
      Message message = messageConsumer.receive(5000);
      if (message == null) {
        System.out.println("Did not receive message within the allotted time.");
        return;
      }
      System.out.println("Received message: " + message);
    } catch (Throwable e) {
      e.printStackTrace();
      return;
    }
  }
}
Şöyle yaparız
InitialContext initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
Connection connection = cf.createConnection();
Thread myConsumer = myMultiThreadedApp.runConsumer(connection, queue);