Session etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Session etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

21 Temmuz 2020 Salı

JMS Session Arayüzü

Giriş
Şu satırı dahil ederiz
import javax.jms.Session;
JMS — Session and two-phase commit (XA) transactions
Açıklaması şöyle
Most JMS message brokers have good support for transactional workloads.

A transacted session supports a single series of transactions. Each transaction groups a set of produced messages and a set of consumed messages into an atomic unit of work.

Two-phase commit transactions (XA transactions) work on a limited scale. They are used to integrate with other systems like Mainframe CICS / DB2 or Oracle database. But it is hard to operate and not possible to scale beyond a few transactions per second.

It is important to note that support for XA transactions is not mandatory with the JMS 2.0 specification. This differs from the session transaction.
constructor
Örnek - AUTO_ACKNOWLEDGE
Şöyle yaparız
private void createUnsharedConsumer(ConnectionFactory connectionFactory, Topic topic)
  throws JMSException {

  Connection connection = connectionFactory.createConnection();
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  ... 
}
createConsumer metodu - Destination
MessageConsumer döndürür

createDurableSubscription metodu
Örnek ver

createMessage metodu
Şöyle yaparız.
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);

Queue testQueue = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(null);


Message msg = session.createMessage();
producer.send(testQueue, msg) 
createProducer metodu
createProducer() her zaman createMessage(), createObjectMessage() ile birlikte kullanılır.

Örnek - Queue'ya Yazma
Şöyle yaparız
ConnectionFactory connectionFactory =
String QUEUE_NAME = "user_creation_queue";

void pushNewUserToQueue(User newUser) throws Exception {
  Session session = ...
  Destination destination = session.createQueue(QUEUE_NAME);
  MessageProducer producer = session.createProducer(destination);

  ObjectMessage message = session.createObjectMessage(newUser);
  producer.send(message);
}
createSharedConsumer metodu - Topic + String
Mesajı okumak için MessageConsuer döndürür. JMS 2.0 ile kullanılır. Destination olarak Queue, TemporaryQueue, TemporaryTopic, Topic kullanılır.

Örnek
Şöyle yaparız.
private void createSharedConsumer(ConnectionFactory connectionFactory, Topic topic)
  throws JMSException {

   Connection connection = connectionFactory.createConnection();

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

   MessageConsumer messageConsumer = session.createSharedConsumer(topic,"mySubscription");

   connection.start();

   Message message = messageConsumer.receive(10000);
   while (message != null) {
      System.out.println("Message received: " + ((TextMessage) message).getText());
      message = messageConsumer.receive(10000);
   }
   connection.close();
}

23 Aralık 2019 Pazartesi

JavaMail Session Sınıfı

getInstance metodu
Şöyle yaparız.
Properties props = new Properties();
...
Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
    }
});

9 Nisan 2018 Pazartesi

WebSocket Session Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.websocket.server.Session;
getBasicRemote metodu
Şöyle yaparız.
Session session = ...;
session.getBasicRemote().sendText(...);

22 Aralık 2016 Perşembe

JavaMail Session Sınıfı

Session Sınıfı
Bu sınıfı kullanmak için şu satır dahil edilir.
import javax.mail.Session;
E-posta için kullanılan bir kaç protokol çeşidir var. Bunlar SMPT (Simple Mail Transfer Protocol), IMAP (Internet Message Access Protocol), POP3 (Post Office Protocol Version 3). Session sınıfının esas amacı bizi bu protokollerden izole etmektir. Session sınıfı, MAPI, NNTP gibi diğer e-posta protokollerini desteklemiyor.

SMTP e-posta göndermek içindir. POP3 sadece e-posta okumak içindir.

Genel kullanım şekli
Örneğin Session sınıf aracılığıyla Transport,Store, gibi nesneleri elde ederiz.
Session session = ...;
MimeMessage message = new MimeMessage(session);
...
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
getInstance metodu
Parametre olarak Properties ve Authenticator alır. Sadece Properties vermek için şöyle yaparız.
Properties props = new Properties();
props.setProperty(...,...);
Session session = Session.getInstance(props, null);
Properties ve Authenticator vermek için şöyle yaparız.
Properties props = new Properties();
prop.put("...", "...");
Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
    }
});
getDefaultInstance metodu
getDefaultInstance() herkesle paylaşıldığı için kullanılmamalı. Metodun için şuna benziyor. defaultSession nesnesinin null ise yaratıldığı mevcut ise tekrar kullanıldığı görülebilir.
public static synchronized Session getDefaultInstance(Properties props,
                                       Authenticator authenticator) {
  if (defaultSession == null)
    defaultSession = new Session(props, authenticator);
  else {
    // have to check whether caller is allowed to see default session
    if (defaultSession.authenticator == authenticator)
               ;       // either same object or both null, either way OK
    else if (defaultSession.authenticator != null &&
             authenticator != null &&
             defaultSession.authenticator.getClass().getClassLoader() ==
             authenticator.getClass().getClassLoader())
               ;       // both objects came from the same class loader, OK
     else
       // anything else is not allowed
       throw new SecurityException("Access to default session denied");
  }
  return defaultSession;
}
getTransport metodu
Şöyle yaparız.
Transport transport = session.getTransport ("smtp");
getStore metodu
Store nesnesi döner. Şöyle yaparız.
Store store = session.getStore();
setDebug metodu
Şöyle yaparız.
session.setDebug(true);

Store Sınıfı
Store sınıfından Folder nesnesi elde ederiz.