1 Mart 2018 Perşembe

JMS ConnectionFactory Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.jms.ConnectionFactory;
JMS sunucusuna bağlanmak için kullanılır.

Bu arayüzü gerçekleştiren sınıflardan birisi ActiveMQ ActiveMQConnectionFactory Sınıfı

constructor
Örnek - 
Şöyle yaparız.
@Bean
public ConnectionFactory getconnFac(InitialContext ctx) throws Exception{
  ConnectionFactory cf = null;
  try{
    cf = (ConnectionFactory) ctx.lookup(jmsProps.getConnectionFactoryJndi());
  }
  catch(NamingException ne){
    throw ne;
  }
  return cf;
}
Örnek
Şöyle yaparız. Bu sefer Spring'e ait JndiObjectFactoryBean nesnesi kullanılıyor.
/**
 * Create Jms Connection factory from Jndi Objectfactory
 * 
 * @param jndiObjectFactoryBean
 *            Jndi Object factory bean
 * @return Returns Jms Connection factory Bean
 */
@Bean(name = "jmsWlsConnectionFactory")
public ConnectionFactory jmsConnectionFactory(final JndiObjectFactoryBean
  jndiObjectFactoryBean) {
  final ConnectionFactory connectionFactory = (ConnectionFactory)
    jndiObjectFactoryBean.getObject();
  LOG.debug("ConnectoinFactory is null? {}", connectionFactory == null);
  return connectionFactory;
}
Örnek - ActiveMQ
Şöyle yaparız
public static void main(String[] args) throws JMSException {
  // Getting JMS connection from the server
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  Topic topic = session.createTopic("topic");
  MessageConsumer consumer = session.createConsumer(topic);

  MessageListener listener = new MessageListener() {
    @Override
    public void onMessage(Message message) {
      try {
        //do operations
      } catch (JMSException e) {
        ...  
      }
     }
  };

  consumer.setMessageListener(listener);
  connection.close();
}
createConnection metodu
JMS Connection arayüzü döner.

Örnek
Şöyle yaparız
ConnectionFactory connectionFactory = ...

Session createActiveMqSession() throws Exception {
  Connection connection = connectionFactory.createConnection();
  connection.start();
  return connection.createSession(false, AUTO_ACKNOWLEDGE);
}

Hiç yorum yok:

Yorum Gönder