1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.protocol.jms.client; 20 21 import java.io.Serializable; 22 import javax.naming.Context; 23 import javax.naming.InitialContext; 24 import javax.naming.NamingException; 25 import javax.jms.JMSException; 26 import javax.jms.ObjectMessage; 27 import javax.jms.TextMessage; 28 import javax.jms.Topic; 29 import javax.jms.TopicConnection; 30 import javax.jms.TopicPublisher; 31 import javax.jms.TopicSession; 32 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.log.Logger; 35 36 public class Publisher { 37 38 private static final Logger log = LoggingManager.getLoggerForClass(); 39 40 private TopicConnection CONN = null; 41 42 private TopicSession SESSION = null; 43 44 private Topic TOPIC = null; 45 46 private TopicPublisher PUBLISHER = null; 47 48 //private byte[] RESULT = null; 49 50 //private Object OBJ_RESULT = null; 51 52 /** 53 * 54 */ 55 public Publisher(boolean useProps, String jndi, String url, String connfactory, String topic, boolean useAuth, 56 String user, String pwd) { 57 super(); 58 Context ctx = initJNDI(useProps, jndi, url, useAuth, user, pwd); 59 if (ctx != null) { 60 initConnection(ctx, connfactory, topic); 61 } else { 62 log.error("Could not initialize JNDI Initial Context Factory"); 63 } 64 } 65 66 public Context initJNDI(boolean useProps, String jndi, String url, boolean useAuth, String user, String pwd) { 67 if (useProps) { 68 try { 69 return new InitialContext(); 70 } catch (NamingException e) { 71 log.error(e.getMessage()); 72 return null; 73 } 74 } else { 75 return InitialContextFactory.lookupContext(jndi, url, useAuth, user, pwd); 76 } 77 } 78 79 public void initConnection(Context ctx, String connfactory, String topic) { 80 try { 81 ConnectionFactory.getTopicConnectionFactory(ctx,connfactory); 82 this.CONN = ConnectionFactory.getTopicConnection(); 83 this.TOPIC = InitialContextFactory.lookupTopic(ctx, topic); 84 this.SESSION = this.CONN.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); 85 this.PUBLISHER = this.SESSION.createPublisher(this.TOPIC); 86 log.info("created the topic connection successfully"); 87 } catch (JMSException e) { 88 log.error("Connection error: " + e.getMessage()); 89 } 90 } 91 92 public void publish(String text) { 93 try { 94 TextMessage msg = this.SESSION.createTextMessage(text); 95 this.PUBLISHER.publish(msg); 96 } catch (JMSException e) { 97 log.error(e.getMessage()); 98 } 99 } 100 101 public void publish(Serializable contents) { 102 try { 103 ObjectMessage msg = this.SESSION.createObjectMessage(contents); 104 this.PUBLISHER.publish(msg); 105 } catch (JMSException e) { 106 log.error(e.getMessage()); 107 } 108 } 109 110 /** 111 * Clise will close the session 112 */ 113 public void close() { 114 try { 115 log.info("Publisher closed"); 116 this.PUBLISHER.close(); 117 this.SESSION.close(); 118 this.CONN.close(); 119 this.PUBLISHER = null; 120 this.SESSION = null; 121 this.CONN = null; 122 } catch (JMSException e) { 123 log.error(e.getMessage()); 124 } catch (Throwable e) { 125 log.error(e.getMessage()); 126 } 127 } 128 129 }