001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.jms.client;
020:
021: import java.io.Serializable;
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025: import javax.jms.JMSException;
026: import javax.jms.ObjectMessage;
027: import javax.jms.TextMessage;
028: import javax.jms.Topic;
029: import javax.jms.TopicConnection;
030: import javax.jms.TopicPublisher;
031: import javax.jms.TopicSession;
032:
033: import org.apache.jorphan.logging.LoggingManager;
034: import org.apache.log.Logger;
035:
036: /**
037: * @author pete
038: *
039: */
040: public class Publisher {
041:
042: private static final Logger log = LoggingManager
043: .getLoggerForClass();
044:
045: private TopicConnection CONN = null;
046:
047: private TopicSession SESSION = null;
048:
049: private Topic TOPIC = null;
050:
051: private TopicPublisher PUBLISHER = null;
052:
053: //private byte[] RESULT = null;
054:
055: //private Object OBJ_RESULT = null;
056:
057: /**
058: *
059: */
060: public Publisher(boolean useProps, String jndi, String url,
061: String connfactory, String topic, String useAuth,
062: String user, String pwd) {
063: super ();
064: Context ctx = initJNDI(useProps, jndi, url, useAuth, user, pwd);
065: if (ctx != null) {
066: initConnection(ctx, connfactory, topic);
067: } else {
068: log
069: .error("Could not initialize JNDI Initial Context Factory");
070: }
071: }
072:
073: public Context initJNDI(boolean useProps, String jndi, String url,
074: String useAuth, String user, String pwd) {
075: if (useProps) {
076: try {
077: return new InitialContext();
078: } catch (NamingException e) {
079: log.error(e.getMessage());
080: return null;
081: }
082: } else {
083: return InitialContextFactory.lookupContext(jndi, url,
084: useAuth, user, pwd);
085: }
086: }
087:
088: public void initConnection(Context ctx, String connfactory,
089: String topic) {
090: try {
091: ConnectionFactory.getTopicConnectionFactory(ctx,
092: connfactory);
093: this .CONN = ConnectionFactory.getTopicConnection();
094: this .TOPIC = InitialContextFactory.lookupTopic(ctx, topic);
095: this .SESSION = this .CONN.createTopicSession(false,
096: TopicSession.AUTO_ACKNOWLEDGE);
097: this .PUBLISHER = this .SESSION.createPublisher(this .TOPIC);
098: log.info("created the topic connection successfully");
099: } catch (JMSException e) {
100: log.error("Connection error: " + e.getMessage());
101: }
102: }
103:
104: public void publish(String text) {
105: try {
106: TextMessage msg = this .SESSION.createTextMessage(text);
107: this .PUBLISHER.publish(msg);
108: } catch (JMSException e) {
109: log.error(e.getMessage());
110: }
111: }
112:
113: public void publish(Serializable contents) {
114: try {
115: ObjectMessage msg = this .SESSION
116: .createObjectMessage(contents);
117: this .PUBLISHER.publish(msg);
118: } catch (JMSException e) {
119: log.error(e.getMessage());
120: }
121: }
122:
123: /**
124: * Clise will close the session
125: */
126: public void close() {
127: try {
128: log.info("Publisher closed");
129: this .PUBLISHER.close();
130: this .SESSION.close();
131: this .CONN.close();
132: this .PUBLISHER = null;
133: this .SESSION = null;
134: this .CONN = null;
135: } catch (JMSException e) {
136: log.error(e.getMessage());
137: } catch (Throwable e) {
138: log.error(e.getMessage());
139: }
140: }
141:
142: }
|