30 Mayıs 2022 Pazartesi

Swing MaskFormatter Sınıfı

Giriş
Şu satırı dahil ederiz. JFormattedTextField ile birlikte kullanılır
import javax.swing.text.MaskFormatter;
Örnek
Şöyle yaparız
JFormattedTextField customerId = new JFormattedTextField(new MaskFormatter("UUU_UUU"));
Mask Örnekleri
UUU_UUU
########-#### - Date
#.# - Product Version

26 Mayıs 2022 Perşembe

JUnit Pioneer İle Birim Testi

Giriş
System.getProperty() şeklindeki çağrıları test etmeyi kolaylaştıyor. Bence kullanması System Stubs'tan çok daha kolay

Gradle
Şöyle yaparız
testImplementation 'org.junit-pioneer:junit-pioneer:1.7.0'
Örnek
Şöyle yaparız
@Test
@ClearSystemProperty(key = "some key")
@SetSystemProperty(key = "another key", value = "new value")
void test() {
  assertNull(System.getProperty("some key"));
  assertEquals("new value", System.getProperty("another key"));
}



19 Mayıs 2022 Perşembe

Helidon

Giriş
Açıklaması şöyle
Helidon is a set of libraries for microservices. It is created and maintained by Oracle. To run it, we need at least Java 11. The latest release introduces support for Java 17. We can write code with Java and Kotlin as well. Interestingly, it comes in two flavors: Helidon SE and Helidon MP.
...
Both may be compiled into native images using GraalVM, resulting in a faster startup time and lower memory consumption. However, the SE profile consumes fewer resources than MP.
İki türevi var. Her ikisi de GraalVM ile çalışıyor
1. Helidon SE
2. Helidon MP

1. Helidon SE
Açıklaması şöyle
Helidon SE is a small toolkit that supports the most recent Java SE features, including reactive streams, asynchronous and functional programming, and APIs with a fluid design. Helidon SE offers GraalVM native files for low storage use and flash -like launch. Helidon SE’s REST framework is based on Netty and provides a simplistic API for session handling.
Açıklaması şöyle
Helidon SE is a toolkit providing reactive streams, asynchronous and functional programming, and fluent-style APIs. It offers a reactive API for streams, messaging, and databases. You may create web endpoints based on GraphQL, gRPC, WebSockets, and HTTP server technologies. Next, we can secure them using a dedicated security add-on. And, at the end, we can spice it with a pinch of metrics, health checks, tracing, and documentation with OpenAPI.
2. Helidon MP
Açıklaması şöyle
On the other hand, Helidon MP is an implementation of MicroProfile specification. Among the features mentioned for the SE profile, you can find here support for JPA, RESTful services, security with JWT, and long-running actions based on SAGA patterns. Furthermore, this version is meant to run in the cloud; we can deploy an application on OCI or Kubernetes.

Both may be compiled into native images using GraalVM, resulting in a faster startup time and lower memory consumption. However, the SE profile consumes fewer resources than MP.

3 Mayıs 2022 Salı

RAbbitMQ API

Channel Sınıfı
Şu satırı dahil ederiz. Bu sınıfın basicPublish(), basicConsume() metodları ile mesaj gönderilir ve alınır
import com.rabbitmq.client.Channel;
constructor
Örnek
Şöyle yaparız
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");

Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
basicConsume metodu
Örnek
Şöyle yaparız
DeliverCallback deliverCallbackDlx = (consumerTag, delivery) -> {
  String routingKey = delivery.getEnvelope().getRoutingKey();
  String message = new String(delivery.getBody(), StandardCharsets.UTF_8);

  System.out.println(" [x] Received WITH DLX! '" + routingKey + "':'" + message + "'");

  channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
};

channel.basicConsume("tickets_dlx", false, deliverCallbackDlx, consumerTag -> {});
basicPublish metodu
Örnek
Açıklaması şöyle
The following shows how RabbitMQ’s Java publisher sets the TTL of a message which can reside in the queue for at most 60 seconds:
Şöyle yaparız
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                                   .expiration("60000")
                                   .build();
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
Örnek
Şöyle yaparız
channel.basicPublish("tickets", "payment_is_done", null,
  paymentInfo.toString().getBytes(StandardCharsets.UTF_8));
exchangeDeclare metodu
Örnek
Şöyle yaparız
channel.exchangeDeclare("tickets", "direct");
queueDeclare metodu
Örnek
Şöyle yaparız. Burada kuyruk için dead letter exchange de tanımlanıyor
Map<String, args = new HashMap<>()
{
  {
    put("x-max-length", 10);
    put("x-message-ttl", 5000);
    put("x-dead-letter-exchange", "tickets_dlx";
    put("x-dead-letter-routing-key", "payment_is_done");
  }
};

channel.queueDeclare("TICKETS_AFTER_APPROVING_PAYMENT", 
  true, //Durable
  false,//Exclusive 
  false, //Auto delete
  args);
channel.queueBind("TICKETS_AFTER_APPROVING_PAYMENT", "tickets", "payment_is_done");