domenica 13 maggio 2012

JMS broker connection sample (running same or different JVM)

CONNECTING TO EXTERNAL JMS  BROKER  (AMQ running in another JVM)


        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(activeMqUrl);
        jmsConnection = connectionFactory.createConnection();
        session = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination notificationTopic = session.createTopic("SYSTEM.INVENTORY.NOTIFICATION.JMSTOPIC");
        MessageConsumer consumer = session.createConsumer(notificationTopic, null);   
        consumer.setMessageListener(this);
        //Mi metto in ascolto sulla coda   
        jmsConnection.start();





USING J2EE COntainer Capabilities - Connecting to J2EE provided broker (JBOSS sample) - same JMV

In this sample we use Jboss naming service (on port 1099) to connect to a Topic

protected Boolean  sendToTopic(NotifMsgJms msg)
 {
      //STEP 1: Ottenere riferimento al Context del Server Jboss
     InitialContext ctx = null;
     String nMethod = "sendToTopic - ";
     try
     {
       Hashtable<String, String> env = new Hashtable<String, String>();
       env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
       env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
       ctx = new InitialContext(env);
     }
     catch (NamingException e)
     {
      log.error(nMethod + "- ERROR: [" + e.getMessage() + "]");
      log.debug(nMethod + " END");
      return false;
     }
    
     try{
       //STEP 2: Ottenere il riferimento alla Connection Factory mediante JNDI
       TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
      
       //STEP 3: Ottenere il riferimento alla Topic mediante JNDI
       Topic topic = (Topic) ctx.lookup(EngineDefines.SB_TOPIC);
      
       //STEP 4: Utilizzare la connection Factory per creare una Connessione
       TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
      
       //STEP 5: Utilizzare la Connessione per creare una Sessione
       TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
      

Nessun commento:

Posta un commento