2011년 5월 23일 월요일

Starting Java Messaging System - 3

JMS PTP Test Program 작성
HelloWorldJmsSender.java
import javax.naming.*;
import javax.jms.*;
import java.util.Properties;
public class HelloWorldJmsSender{
public final static String JMS_FACTORY="QueueConnectionFactory";
public final static String QUEUE="ExampleQueue";
public static void main(String args[]) {
try {
QueueConnectionFactory queueConnectionFactory;
QueueConnection queueConnection;
QueueSession queueSession;
QueueSender queueSender;
Queue queue;
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 Queue Look up 합니다
queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
queue = (Queue) ctx.lookup(QUEUE);
// connection 작성
queueConnection = queueConnectionFactory.createQueueConnection();
// 세션을 작성
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// sender 작성
queueSender = queueSession.createSender(queue);
// 메세지를 작성
msg = queueSession.createTextMessage();
msg.setText("Hello World hurukku!!");
// 메세지의 배송을 스타트
queueConnection.start();
// 메세지를 송신
queueSender .send(msg);
// 접속을 close
queueSender.close();
queueSession.close();
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

HelloWorldJmsReceiver.java
import javax.jms.*;
import javax.naming.*;
import java.util.Properties;
public class HelloWorldJmsReceiver {
public final static String JMS_FACTORY="QueueConnectionFactory";
public final static String QUEUE="ExampleQueue";
public static void main(String[] args) {
try {
QueueConnectionFactory queueConnectionFactory;
QueueConnection queueConnection;
QueueSession queueSession;
QueueReceiver queueReceiver;
Queue queue;
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 Queue Look up 합니다
queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
queue = (Queue) ctx.lookup(QUEUE);
// connection 작성
queueConnection = queueConnectionFactory.createQueueConnection();
// 세션을 작성
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// 리시버를 작성
queueReceiver = queueSession.createReceiver(queue);
// 메세지의 배송을 스타트
queueConnection.start();
// 메세지의 수신
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
msg = (TextMessage) m;
System.out.println(msg.getText());
} else {
break;
}
}
}
// 접속을 close
queueReceiver.close();
queueSession.close();
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Source Compile 및 실행
- Compile
“javac -classpath /home/craftlee/jeus/lib/system/jeus.jar *.java”
- Sender 실행
“java -classpath .:/home/craftlee/jeus/lib/system/jeus.jar:/home/craftlee/jeus/lib/system/jmxri.jar HelloWorldJmsSender”
jmsadmin에서 엔진모니터링
Destination Type : Queue
Name of Destination : ExampleQueue
JNDI name of Destination : ExampleQueue
Client information that is consuming this destination
-----------------------------------------------------
No client is consuming this destination.
number of messages remained in Queue : 1

- Receiver 실행
“java -classpath .:/home/craftlee/jeus/lib/system/jeus.jar:/home/craftlee/jeus/lib/system/jmxri.jar HelloWorldJmsReceiver”
Destination Type : Queue
Name of Destination : ExampleQueue
JNDI name of Destination : ExampleQueue
Client information that is consuming this destination
-----------------------------------------------------
client 0 : Address; 127.0.0.1, port; 34001
clientNumber; 0
number of messages remained in Queue : 0

댓글 없음:

댓글 쓰기

블록체인 개요 및 오픈소스 동향

블록체인(block chain) 블록체인은 공공 거래장부이며 가상 화폐로 거래할때 발생할때 발생할 수 있는 해킹을 막는 기술. 분산 데이터베이스의 한 형태로, 지속적으로 성장하는 데이터 기록 리스트로서 분산 노드의 운영자에 의한 임의 조작이 불가...