2011년 5월 23일 월요일

Starting Java Messaging System - 4

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”

댓글 없음:

댓글 쓰기

ETL 솔루션 환경

ETL 솔루션 환경 하둡은 대용량 데이터를 값싸고 빠르게 분석할 수 있는 길을 만들어줬다. 통계분석 엔진인 “R”역시 하둡 못지 않게 관심을 받고 있다. 빅데이터 역시 데이터라는 점을 볼때 분산처리와 분석 그 이전에 데이터 품질 등 데이...