본문으로 바로가기

[JMS]Activemq 메시지 생성자(Producer)

category JAVA 2019. 2. 18. 09:12
반응형

class ActivemqStart { private Destination destination; // 목적지설정 private int messageCount = 1; // 메시지 카운트 10 private long sleepTime; private int messageSize = 255; //메시지 크기 private long messageLive; private String url = " "; // activemq 접속 url private String route = ""; // topic 경로 private boolean persistent; public void run() { Connection conn = null; try { System.out.println("연결된 주소 : " + url ); System.out.println(messageSize + " " + route); System.out.println((persistent ? "persistent": "non-persistent" ) + " message"); System.out.println("실행시간 " + sleepTime + " ms"); if(messageLive != 0) { System.out.println("메시지 지속 시간 " + messageLive + " ms"); } //ActivemMQ 연결팩토리 생성 ActiveMQConnectionFactory CF = new ActiveMQConnectionFactory(url); //연결 conn = CF.createConnection(); conn.start(); //세션 연결 /** * AUTO.ACKNOWLEDGE * 자동으로 수신확인 */ Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createTopic(route); //메시지 프로듀서 생성(topic 경로) MessageProducer mp = null; mp = session.createProducer(destination); /** * SetDeliveryMode * JMS 중단 시 전송 메시지 유지 및 실종 여부 메소드 * NON_PERSISTENT -> 메시지 실종 */ mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT); Send(session, mp); System.out.println("정상"); }catch(Exception e){ e.printStackTrace(); }finally { try{ System.out.println("곧 종료"); conn.close(); }catch(Exception e) { e.printStackTrace(); } } } public void Send(Session session, MessageProducer mp) throws Exception{ for(int i =0; i<messageCount ; i++) { // 메시지 카운트보다 커질 떄가지 반복 // 메시지 전송 String text = "Success"; TextMessage message = session.createTextMessage(text); mp.send(message); System.out.println(message); } } public class MQProduce { public static void main(String args[]) throws JMSException{ ActivemqStart Acs = new ActivemqStart(); Acs.run(); } }


반응형