jms2pub2sub 모드

2985 단어 중간부품jmsejb
서비스 측은 두 개의 소비자를 정의했다
 
@MessageDriven(activationConfig={
		@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"),
		@ActivationConfigProperty(propertyName="destination",propertyValue="topic/myTopic")
}
)
public class TopicMessageBean01 implements MessageListener {

	public void onMessage(Message msg) {
		TextMessage txtMsg=(TextMessage)msg;
		String s="";
		try {
			s = txtMsg.getText();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("TopicMessageBean01 :"+s);

	}

}

 
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig={
		@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"),
		@ActivationConfigProperty(propertyName="destination",propertyValue="topic/myTopic")
}
)
public class TopicMessageBean02 implements MessageListener {

	public void onMessage(Message msg) {
		TextMessage txtMsg=(TextMessage)msg;
		String s="";
		try {
			s = txtMsg.getText();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("TopicMessageBean02 :"+s);

	}

}

 
 
 
 
클라이언트 게시자 코드
 
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;

public class Test {
	public static void main(String[] args) throws Exception {
		InitialContext context=new InitialContext();
		// QueueConnectionFactory
		TopicConnectionFactory factory =(TopicConnectionFactory)context.lookup("TopicConnectionFactory");
		// QueueConnection
		TopicConnection conn=factory.createTopicConnection();
		// 
		//p1:  true:   false: 
		//p2: , 
		TopicSession session=conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
		// destination
		Topic topic=(Topic)context.lookup("topic/myTopic");
		// 
		MessageProducer publisher=session.createProducer(topic);
		
		// 
		Message msg=session.createTextMessage(" ");
		// 
		publisher.send(topic, msg);
		
		session.close();
		conn.close();
	}
}

좋은 웹페이지 즐겨찾기