12 Ekim 2023 Perşembe

RabbitMQ Client Kullanımı

Örnek
Şu satırı dahil ederiz
<dependency>
<groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>rabbitmq</artifactId> <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.rabbitmq.jms</groupId> <artifactId>rabbitmq-jms</artifactId> <version>3.1.0</version> </dependency>
Şöyle yaparız
import com.rabbitmq.jms.admin.RMQConnectionFactory;
import org.testcontainers.containers.RabbitMQContainer;
import jakarta.jms.ConnectionFactory;


RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.8");
rabbitMQContainer.start();

RMQConnectionFactory f = new RMQConnectionFactory();
f.setUri(rabbitMQContainer.getAmqpUrl());
...
rabbitMQContainer.stop();

17 Eylül 2023 Pazar

Embedded Tomcat

Giriş
Şu satırı dahil ederiz.
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-core</artifactId>
  <version>${tomcat.version}</version>
</dependency>
Tomcat Sınıfı
addServlet metodu
Şöyle yaparız.
Tomcat tomcat = new Tomcat();
tomcat.setPort(8082);
tomcat.addContext("/", "/web");
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());

Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    Writer w = resp.getWriter();
    w.write("Embedded Tomcat servlet.\n");
    w.flush();
    w.close();
  }
});

ctx.addServletMappingDecoded("/*", "Embedded");

tomcat.start();
tomcat.getServer().await();
setBaseDir metodu
Örnek
Şöyle yaparız
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

public class TomcatEmbedded {

  public static void main(String... args) throws Exception {
    File baseFolder = new File(System.getProperty("user.dir"));
    File appsFolder = new File(baseFolder, "apps");

    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir(baseFolder.getAbsolutePath());
    tomcat.setPort(8080);
    tomcat.getHost().setAppBase(appsFolder.getAbsolutePath());

    // Call the connector to create the default connector.
    tomcat.getConnector();

    tomcat.addWebapp("", ".");
    Wrapper wrapper = tomcat.addServlet(EMPTY, "hello", new HelloServlet());
    wrapper.setLoadOnStartup(1);
    wrapper.addMapping("/*");

    tomcat.start();
    tomcat.getServer().await();
  }

  private static class HelloServlet extends HttpServlet {
    ...
}

getService metodu
Elimizde şöyle bir kod olsun
public class CustomLoggerValve extends ValveBase {
  private static final Logger logger = LoggerFactory.getLogger(CustomLoggerValve.class);

  @Override
  public void invoke(Request request, Response response)
  throws IOException, ServletException {
    try {
      MDC.put("requestId", UUID.randomUUID().toString());
      logger.info("Received request");
      getNext().invoke(request, response);
    } finally {
      MDC.remove("requestId");
    }
  }
}
Şöyle yaparız
Tomcat tomcat = // ... your tomcat setup
tomcat.getService().getContainer().getPipeline().addValve(new CustomLoggerValve());

11 Eylül 2023 Pazartesi

Jetty Kullanımı

Maven
Şu satırı dahile deriz
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-bom</artifactId>
  <version>9.4.51.v20230217</version>
  <scope>import</scope>
  <type>pom</type>
</dependency>

23 Ağustos 2023 Çarşamba

Docker ve Artemis

Örnek
Şöyle yaparız
docker run --rm \ -e AMQ_EXTRA_ARGS="--http-host 0.0.0.0 --relax-jolokia" \ -e AMQ_USER=admin \ -e AMQ_PASSWORD=admin \ -p 61616:61616 \ -p 8161:8161 \ --name artemis \
quay.io/artemiscloud/activemq-artemis-broker
Bağlanmak için şöyle yaparız. Kullanıcı adı ve şifre yukarıda belirtiliyor

15 Ağustos 2023 Salı

Docker ve ActiveMQ

Örnek
Şöyle yaparız
> docker run -d --name activemq -p 61616:61616 -p 8161:8161 webcenter/activemq
Daha sonra şu adrese gideriz
Kullanıcı adı : admin Şifre : admin

2 Ağustos 2023 Çarşamba

Java Transaction API - JTA - Distributed Transaction İçindir

Giriş
Açıklaması şöyle
The Java Transaction API (JTA) is one of the APIs defined in the Java Enterprise Edition (Java EE) platform, specifically providing a framework for handling distributed transactions in a consistent and reliable manner.
Açıklaması şöyle
The Java Transaction API consists of three key elements:

UserTransaction interface: This provides the application with the ability to control transaction boundaries programmatically.
TransactionManager interface: It offers more advanced features, like suspending, resuming transactions, and is typically used by the container rather than by the application.
XAResource interface: This is the bridge between the resource manager (like a database) and the transaction manager. It includes methods for associating a resource with a transaction and for transaction completion.
UserTransaction yazısına bakabilirsiniz

JTA UserTransaction Arayüzü

Giriş
Eski kodlarda şu satırı dahil ederiz.
import javax.transaction.UserTransaction;
Şu satırı dahil ederiz
import jakarta.transaction.UserTransaction;
Açıklaması şöyle
UserTransaction interface: This provides the application with the ability to control transaction boundaries programmatically.
Maven
API için şu satırı dahil deriz
<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>javax.transaction-api</artifactId>
  <version>1.3</version>
</dependency>

Örnek
Şöyle yaparız
@Resource
UserTransaction utx;

public void transferFunds(Account debitAccount, Account creditAccount, double amount) 
throws Exception {
  try {
    utx.begin();
        
    debitAccount.withdraw(amount);
    creditAccount.deposit(amount);

    utx.commit();
  catch (Exception e) {
   utx.rollback();
   throw e;
  }
}

Bean Validation @GroupSequence Anotasyonu

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