ÃֽŠ°Ô½Ã±Û(JAVA)
2017.06.12 / 13:58

JMS °³³ä ¹× Spring¿¡¼­ Áö¿øÇÏ´Â JMS, MDP

Ŭ·¡½Ä·Î¾â
Ãßõ ¼ö 204

JMS °³³ä ¹× Spring¿¡¼­ Áö¿øÇÏ´Â JMS, MDP

Table of Contents

¿¹Á¦

¾Æ·¡ ¿¹Á¦¿¡¼­ ÁÖ°¡ Á¤º¸¸¦ ÁÖ°í ¹Þ´Â Å¬¶óÀ̾ðÆ® ¾ÖÇø®ÄÉÀ̼ÇÀ» »ìÆì º¸ÀÚ.

  1. ¸Þ¼¼Áö¸¦ º¸³»°í ¹Þ±â À§ÇÑ ¼±ÇàÀÛ¾÷
    • Getting a ConnectionFactory
      import javax.naming.*;
      import javax.jms.*;
      ConnectionFactory connectionFactory;
      Context messaging = new InitialContext();
      connectionFactory = (ConnectionFactory)messaging.lookup("ConnectionFactory");
      
    • Getting a Destination
      Queue stockQueue;
      stockQueue = (Queue)messaging.lookup("StockSource");
      
    • Creating a Connection
      Connection connection;
      connection = ConnectionFactory.createConnection();
      
    • Creating a Session
      Session session;
      
      /* Session is not transacted,
       * uses AUTO_ACKNOWLEDGE for message
       * acknowledgement
       */
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      
    • Creating a MessageProducer
      MessageProducer sender;
      
      /* Value in stockQueue previously looked up in the JNDI
       * createProducer takes a Destination
       */
      sender = session.createProducer(stockQueue);
      
    • Creating a MessageConsumer
      MessageConsumer receiver;
      
      /* Value in stockQueue previously looked up in the JNDI
       * createConsumer takes a Destination
       */
      receiver = session.createConsumer(stockQueue);
      
    • Starting Message Delivery
      connection.start();
      
    • Using a TextMessage
      String stockData; /* Stock information as a string */
      TextMessage message;
      
      /* Set the message's text to be the stockData string */
      message = session.createTextMessage();
      message.setText(stockData);
      
  2. ¸Þ¼¼Áö ÁÖ°í ¹Þ±â
    • ¸Þ¼¼Áö º¸³»±â
      /* Send the message */
      sender.send(message);
      
    • ¸Þ¼¼Áö¸¦ µ¿±âÀûÀ¸·Î ¹Þ±â
      TextMessage stockMessage;
      stockMessage = (TextMessage)receiver.receive();
      
      TextMessage stockMessage;
      
      /* Wait 4 seconds for a message */
      TextMessage = (TextMessage)receiver.receive(4000);
      
    • TextMessage¿¡¼­ Á¤º¸ ÃßÃâÇϱâ
      String newStockData; /* Stock information as a string */
      newStockData = message.getText();
      
  3. ±×¹ÛÀÇ ¸Þ¼¼Â¡ °ü·ÃÇÏ¿© ¾Ë¾Æ µÑ »çÇ×µé
    • ¸Þ¼¼Áö¸¦ ºñµ¿±âÀûÀ¸·Î ¹Þ±â
      import javax.jms.*;
      public class StockListener implements MessageListener
      {
          public void onMessage(Message message) {
              /* Unpack and handle the messages received */
              ...
          }
      }
      
      StockListener myListener = new StockListener();
      
      /* Receiver is MessageConsumer object */
      receiver.setMessageListener(myListener);
      
      public void onMessage(Message message)
      {
          String newStockData;
          /* Unpack and handle the messages received */
          newStockData = message.getText();
          if(...)
          {
              /* Logic related to the data */
          }
      }
      
    • Message Selection »ç¿ëÇϱâ
      String stockData; /* Stock information as a String */
      TextMessage message;
      
      /* Set the message's text to be the stockData string */
      message = session.createTextMessage();
      message.setText(stockData);
      
      /* Set the message property 'StockSector' */
      message.setStringProperty("StockSector", "Technology");
      
      String selector;
      selector = new String("(StockSector = 'Technology')");
      
      MessageConsumer receiver;
      receiver = session.createConsumer(stockQueue, selector);
      
    • Durable Subcription »ç¿ëÇϱâ
      import javax.naming.*;
      import javax.jms.*;
      
      /* Look up connection factory */
      ConnectionFactory connectionFactory;
      Context messaging = new InitialContext();
      connectionFactory = (ConnectionFactory) Messaging.lookup("ConnectionFactory")
      
      /* Look up destination */
      Topic newsFeedTopic;
      newsFeedTopic = messaging.lookup("BreakingNews");
      
      /* Create connection and session */
      Connection connection;
      Session session;
      connection = ConnectionFactory.createConnection();
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      
      session.createDurableSubscriber(newsFeedTopic,"mySubscription");
      
      /* Reconnect to a durable subscription */
      session.createDurableSubscriber(newsFeedTopic, "mySubscription");
      
  4. JMS ¸Þ¼¼Áö ŸÀÔ
    • TextMessage
      • »ý¼ºÇϱâ
        String stockData; /* Stock information as a string */
        TextMessage message;
        message = session.createTextMessage();
        
        /* Set the stockData string to the message body */
        message.setText(stockData);
        
      • Á¤º¸ ÃßÃâÇϱâ
        String stockInfo; /* String to hold stock info */
        stockInfo = message.getText();
        
    • BytesMessage
      • »ý¼ºÇϱâ
        byte[] stockData; /* Stock information as a byte array */
        BytesMessage message;
        message = session.createBytesMessage();
        message.writeBytes(stockData);
        
      • Á¤º¸ ÃßÃâÇϱâ
        byte[] stockInfo; /* Byte array to hold stock information */
        int length;
        length = message.readBytes(stockInfo);
        
    • MapMessage
      • »ý¼ºÇϱâ
        String stockName; /* Name of the stock */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time the stock quote was updated */
        double stockDiff; /* the +/- change in the stock quote*/
        String stockInfo; /* Other information on this stock */
        MapMessage message;
        
        message = session.createMapMessage();
        
        message.setString("Name", "SUNW");
        message.setDouble("Value", stockValue);
        message.setLong("Time", stockTime);
        message.setDouble("Diff", stockDiff);
        message.setString("Info", "Recent server announcement causes market interest");
        
      • Á¤º¸ ÃßÃâÇϱâ
        String stockName; /*Name of the stock */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time of the stock update */
        double stockDiff; /* +/- change in the stock */
        String stockInfo; /* Information on this stock */
        
        stockName = message.getString("Name");
        stockDiff = message.getDouble("Diff");
        stockValue = message.getDouble("Value");
        stockTime = message.getLong("Time");
        
    • StreamMessage
      • »ý¼ºÇϱâ
        String stockName; /* Name of the stock */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time of the stock update */
        double stockDiff; /* +/- change in the stock quote */
        String stockInfo; /* Information on this stock*/
        StreamMessage message;
        
        /* Create message */
        message = session.createStreamMessage();
        
        /* Set data for message */
        message.writeString(stockName);
        message.writeDouble(stockValue);
        message.writeLong(stockTime);
        message.writeDouble(stockDiff);
        message.writeString(stockInfo);
        
      • Á¤º¸ ÃßÃâÇϱâ
        String stockName; /* Name of the stock quote */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time of the stock update */
        double stockDiff; /* +/- change in the stock quote */
        String stockInfo; /* Information on this stock */
        
        stockName = message.readString();
        stockValue = message.readDouble();
        stockTime = message.readLong();
        stockDiff = message.readDouble();
        stockInfo = message.readString();
        
    • ObjectMessage
      • »ý¼ºÇϱâ
        String stockName; /* Name of the stock quote */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time of the stock update */
        double stockDiff; /* +/- change in the stock quote */
        String stockInfo; /* Information on this stock */
        
        /* Create a StockObject */
        StockObject stockObject = new StockObject();
        
        /* Establish the values for the StockObject */
        stockObject.setName(stockName);
        stockObject.setValue(stockValue);
        stockObject.setTime(stockTime);
        stockObject.setDiff(stockDiff);
        stockObject.setInfo(stockInfo);
        
        /* Create an ObjectMessage */
        ObjectMessage message;
        message = session.createObjectMessage();
        
        /* Set the body of the message to the StockObject */
        message.setObject(stockObject);
        
      • Á¤º¸ ÃßÃâÇϱâ
        StockObject stockObject;
        /* Retrieve the StockObject from the message */
        stockObject = (StockObject)message.getObject();
        
        /* Extract data from the StockObject by using StockObject methods */
        String stockName; /* Name of the stock quote */
        double stockValue; /* Current value of the stock */
        long stockTime; /* Time of the stock update */
        double stockDiff; /* +/- change in the stock quote */
        String stockInfo; /* Information on this stock */
        
        stockName = stockObject.getName();
        stockValue = stockObject.getValue();
        stockTime = stockObject.getTime();
        stockDiff = stockObject.getDiff();
        stockInfo = stockObject.getInfo();
        

JMS Spec

  1. ¼Ò°³
    • JMS´Â ÀÚ¹Ù ÇÁ·Î±×·¥ÀÌ ¿£ÅÍÇÁ¶óÀÌÁî ¸Þ½Ã¡ ½Ã½ºÅÛÀÇ ¸Þ¼¼Áö¸¦ »ý¼º, º¸³»±â, ¹Þ±â, Àбâ À§ÇÑ °øÅëÀÇ ¹æ¹ýÀ» Á¦°øÇÑ´Ù.
    • ¿£ÅÍÇÁ¶óÀÌÁî ¸Þ¼¼Â¡ »óÇ°(ÈçÈ÷ MOM(Message Oriented Middleware) »óÇ°À̶ó°í ¸»ÇÑ´Ù.)Àº intra-company operationsÀ» ÅëÇÕÇϱâ À§ÇÑ ÁÖ¿ä ÄÄÆ÷³ÍÆ®ÀÌ´Ù. ÀÌ·± »óÇ°µéÀº ¼­·Î ¶³¾îÁø ºñÁî´Ï½º ÄÄÆ÷³ÍÆ®µéÀ» reliableÇϸ鼭µµ flexibleÇÏ°Ô ¿«¾îÁØ´Ù.
      ÀÚ¹Ù ¾ð¾î·Î µÈ Ŭ¶óÀ̾ðÆ®µé°ú ÀÚ¹Ù ¾ð¾îÀÇ ¹Ìµé Ƽ¾î ¼­ºñ½ºµéÀº ÀÌ·¯ÇÑ ¸Þ¼¼Â¡ ½Ã½ºÅÛÀ» ÀÌ¿ëÇÒ ¼ö ÀÖ¾î¾ß Çϴµ¥, À̶§ JMS°¡ ÀÌ·¯ÇÑ ½Ã½ºÅÛ¿¡ Á¢±ÙÇϱâ À§ÇÑ °øÅë ¹æ½ÄÀ» Á¤ÀÇÇÏ°í ÀÖ´Â °ÍÀÌ´Ù.
      JMS´Â ÀÎÅÍÆäÀ̽º ?V°ú JMS Ŭ¶óÀ̾ðÆ®°¡ ¿£ÅÍÇÁ¶óÀÌÁî ¸Þ½Ã¡ »óÇ°ÀÇ facilities¿¡ Á¢±ÙÇÏ´Â ¹æ½ÄÀ» Á¤ÀÇÇÑ associated semantics¸¦ °¡Áö°í ÀÖ´Ù.
    • ¸Þ¼¼Â¡Àº peer-to-peerÀÓÀ¸·Î JMSÀÇ ¸ðµç »ç¿ëÀÚ´Â ÀϹÝÀûÀ¸·Î Ŭ¶óÀ̾ðÆ®·Î »ý°¢ÇÏ¸é µÈ´Ù. JMS ¾ÖÇø®ÄÉÀ̼ÇÀº ¾ÖÇø®ÄÉÀ̼ǿ¡¼­ Á¤ÀÇÇÑ ¸Þ¼¼Áö¿Í ±×°ÍÀ» ÁÖ°í ¹Þ´Â Å¬¶óÀ̾ðÆ® ?VÀ¸·Î ±¸¼ºµÈ´Ù. JMS¸¦ ±¸ÇöÇÏ°í ÀÖ´Â »óÇ°µéÀº JMS ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÑ provider¸¦ Á¦°øÇÑ´Ù.
      • ¿©±â¼­ ¸Þ¼¼Áö¶õ (»ç¶÷ÀÌ ¾Æ´Ñ)¿£ÅÍÇÁ¶óÀÌÁî ¾ÖÇÁ¸®ÄÉÀÌ¼Ç °£ÀÇ ºñµ¿±â Ä¿¹Â´ÏÄÉÀ̼ÇÀ» ¸»ÇÑ´Ù.
  2. ¾ÆÅ°ÅØó
    • JMS application ±¸¼º¿ä¼Ò
      • JMS Clients - These are the Java language programs that send and receive messages.
      • Non-JMS Clients - These are clients that use a message system's native client API instead of JMS. If the application predated the availability of JMS it is likely that it will include both JMS and non-JMS clients.
      • Messages - Each application defines a set of messages that are used to communicate information between its clients.
      • JMS Provider - This is a messaging system that implements JMS in addition to the other administrative and control functionality required of a fullfeatured messaging product.
      • Administered Objects - Administered objects are preconfigured JMS objects
        created by an administrator for the use of clients.
        #*There are two types of JMS administered objects:
      • ConnectionFactory - This is the object a client uses to create a connection
        with a provider.
      • Destination - This is the object a client uses to specify the destination of
        messages it is sending and the source of messages it receives.
    • Two Messaging Styles
      • JMS Point-to-Point Model - Queue
      • JMS Publish/Subscribe Model - Topic
    • JMS ¿ÀºêÁ§Æ®°£ °ü°è
      • ConnectionFactory - an administered object used by a client to create a Connection
      • Connection - an active connection to a JMS provider
      • Destination - an administered object that encapsulates the identity of a message destination
      • Session - a single-threaded context for sending and receiving messages
      • MessageProducer - an object created by a Session that is used for sending messages to a destination
      • MessageConsumer - an object created by a Session that is used for receiving messages sent to a destination
    • A typical JMS client executes the following JMS setup procedure:
      • Use JNDI to find a ConnectionFactory object
      • Use JNDI to find one or more Destination objects
      • Use the ConnectionFactory to create a JMS Connection with message delivery inhibited
      • Use the Connection to create one or more JMS Sessions
      • Use a Session and the Destinations to create the MessageProducers and MessageConsumers needed
      • Tell the Connection to start delivery of messages
        At this point a client has the basic JMS setup needed to produce and consume
        messages.
    • Multithreading
      • ¼¼¼Ç¿¡ ´ëÇØ µ¿½Ã Á¢±ÙÀ» ¸·´Â ÀÌÀ¯¿¡´Â µÎ°¡Áö ÀÌÀ¯°¡ ÀÖ´Ù.
        ¼¼¼ÇÀº Æ®·¢Àè¼ÇÀ» Áö¿øÇÏ´Â ¿£Æ¼Æ¼Àε¥, ¸ÖƼ ½º·¹µåÀÎ Æ®·£Àè¼ÇÀ» ±¸ÇöÇÏ´Â °ÍÀº ¸Å¿ì ¾î·Æ±â ¶§¹®ÀÌ´Ù.
        ¼¼¼ÇÀº ºñµ¿±â ¸Þ¼¼Áö ¼Òºñ¸¦ Áö¿øÇϴµ¥, JMS´Â ¿©·¯°³ÀÇ ¸Þ¼¼Áö¸¦ µ¿½Ã¿¡ Çڵ鸵ÇÏ´Â °É ¿ä±¸ÇÏÁö ¾Ê´Â´Ù´Â Á¡ÀÌ Áß¿äÇÏ´Ù.
        µ¿½Ã¼ºÀÌ ¿ä±¸µÈ´Ù¸é, ¿©·¯°³ÀÇ ¼¼¼ÇÀ» ¸¸µé¾î ó¸®ÇÏ¸é µÉ °ÍÀÌ´Ù.
  3. JMS Common Facilities

Spring Ref. Ch.19

ProSpring Ã¥ Á¤¸®

Spring In Action Ã¥ Á¤¸®

JMS Provider - ActiveMQ

Âü°íÀÚ·á

Pro Spring å p.467~486
Spring ·¹ÆÛ·±½º JMS ¹ø¿ª
Spring ·¹ÆÛ·±½º JMS
Asynchronous Messaging Made Easy With Spring JMS
Spring 2.0's JMS Improvements
Spring 2.0's JMS Improvements ¹ø¿ª
1-2-3 messaging with Spring JMS
J2EE Java Message Service (JMS)
JMS in WikipediA

¹®¼­¿¡ ´ëÇÏ¿©

ÃÖÃÊÀÛ¼ºÀÚ : ±è¹ÎÀç
ÃÖÃÊÀÛ¼ºÀÏ : 2006³â 7¿ù 22ÀÏ
¹öÀü : 0.1
¹®¼­ÀÌ·Â :