달력

072018  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  
  •  
  •  
JMS pub/sub Test Program 작성
 
HelloWorldJmsPublisher.java
import javax.naming.*;
import javax.jms.*;
import java.util.Properties;
 
public class HelloWorldJmsPublisher {
  public final static String JMS_FACTORY="TopicConnectionFactory";
  public final static String TOPIC="ExampleTopic";
 
  public static void main(String[] args) {
    try {
      TopicConnectionFactory topicConnectionFactory;
      TopicConnection topicConnection;
      TopicSession topicSession;
      TopicPublisher topicPublisher;
      Topic topic;
      TextMessage msg;
 
      // JNDI InitialContext 작성합니다
      Properties env = new Properties();
      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "jeus.jndi.JEUSContextFactory");
      env.setProperty(Context.PROVIDER_URL, "localhost");
      InitialContext ctx = new InitialContext(env);
 
      // Connection Factory Topic Look up 합니다
      topicConnectionFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
      topic = (Topic) ctx.lookup(TOPIC);
      // connection 작성
      topicConnection = topicConnectionFactory.createTopicConnection();
      // 세션을 작성
      topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      // 토픽 발행자를 작성
      topicPublisher = topicSession.createPublisher(topic);
      // 메세지를 작성
      msg = topicSession.createTextMessage();
      msg.setText("Hell World hurukku Topic JMS");
      // 메세지를 송신
      topicPublisher.publish(msg);
      // 접속을 닫습니다
      topicPublisher.close();
      topicSession.close();
      topicConnection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
HelloWorldJmsSubscriber.java
import javax.naming.*;
import javax.jms.*;
import java.util.Properties;
 
public class HelloWorldJmsSubscriber {
  public final static String JMS_FACTORY="TopicConnectionFactory";
  public final static String TOPIC="ExampleTopic";
 
  public static void main(String[] args) {
    try {
      TopicConnectionFactory topicConnectionFactory;
      TopicConnection topicConnection;
      TopicSession topicSession;
      TopicSubscriber topicSubscriber;
      Topic topic;
      TextMessage msg;
 
      // JNDI InitialContext 작성합니다
      Properties env = new Properties();
      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "jeus.jndi.JEUSContextFactory");
      env.setProperty(Context.PROVIDER_URL, "localhost");
      InitialContext ctx = new InitialContext(env);
 
      // Connection Factory Topic Look up 합니다
      topicConnectionFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
      topic = (Topic) ctx.lookup(TOPIC);
      // connection 작성
      topicConnection = topicConnectionFactory.createTopicConnection();
      // 세션을 작성
      topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      // 트픽크사브스크라이바를 작성
      topicSubscriber = topicSession.createSubscriber(topic);
      // 메세지의 배송을 스타트
      topicConnection.start();
      // 메세지의 수신
      msg = (TextMessage) topicSubscriber.receive();
      System.out.println(msg.getText());
      // 접속을 닫습니다
      topicSubscriber.close();
      topicSession.close();
      topicConnection.close();
    } catch ( Exception e ) {
      e.printStackTrace();
    }
  }
}
 
Source Compile 및 실행
 
-         Compile
“javac -classpath /home/craftlee/jeus/lib/system/jeus.jar *.java”
 
-         Subscriber 실행
“java -classpath .:/home/craftlee/jeus/lib/system/jeus.jar:/home/craftlee/jeus/lib/system/jmxri.jar HelloWorldJmsSubscriber”
 
-         Publisher 실행 (새로운 창으로 실행)
“java -classpath .:/home/craftlee/jeus/lib/system/jeus.jar:/home/craftlee/jeus/lib/system/jmxri.jar HelloWorldJmsPublisher”
Posted by Tornado tornado