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: package org.apache.jmeter.protocol.jms.sampler;
019:
020: import javax.jms.JMSException;
021: import javax.jms.Message;
022: import javax.jms.MessageListener;
023: import javax.jms.TextMessage;
024:
025: import org.apache.jmeter.samplers.Entry;
026: import org.apache.jmeter.samplers.SampleResult;
027: import org.apache.jmeter.testelement.TestListener;
028: import org.apache.jmeter.engine.event.LoopIterationEvent;
029:
030: import org.apache.jmeter.protocol.jms.control.gui.JMSSubscriberGui;
031: import org.apache.jmeter.protocol.jms.client.ClientPool;
032: import org.apache.jmeter.protocol.jms.client.OnMessageSubscriber;
033: import org.apache.jmeter.protocol.jms.client.ReceiveSubscriber;
034:
035: import org.apache.jorphan.logging.LoggingManager;
036: import org.apache.log.Logger;
037:
038: /**
039: *
040: * To change the template for this generated type comment go to
041: * Window>Preferences>Java>Code Generation>Code and Comments
042: */
043: public class SubscriberSampler extends BaseJMSSampler implements
044: TestListener, MessageListener {
045:
046: // private Subscriber SUBSCRIBER = null;
047: private static final Logger log = LoggingManager
048: .getLoggerForClass();
049:
050: private transient ReceiveSubscriber SUBSCRIBER = null;
051:
052: private StringBuffer BUFFER = new StringBuffer();
053:
054: private transient int counter = 0;
055:
056: private transient int loop = 0;
057:
058: private transient boolean RUN = true;
059:
060: private static final String CLIENT_CHOICE = "jms.client_choice"; // $NON-NLS-1$
061:
062: public SubscriberSampler() {
063: }
064:
065: public void testEnded(String test) {
066: testEnded();
067: }
068:
069: public void testStarted(String test) {
070: testStarted();
071: }
072:
073: /**
074: * testEnded is called by Jmeter's engine. the implementation will reset the
075: * count, set RUN to false and clear the StringBuffer.
076: */
077: public synchronized void testEnded() {
078: log.info("SubscriberSampler.testEnded called");
079: this .RUN = false;
080: this .resetCount();
081: ClientPool.clearClient();
082: this .BUFFER = null;
083: if (this .SUBSCRIBER != null) {
084: this .SUBSCRIBER = null;
085: }
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see junit.framework.TestListener#startTest(junit.framework.Test)
092: */
093: public void testStarted() {
094: }
095:
096: public void testIterationStart(LoopIterationEvent event) {
097: }
098:
099: /**
100: * Create the OnMessageSubscriber client and set the sampler as the message
101: * listener.
102: *
103: */
104: public synchronized OnMessageSubscriber initListenerClient() {
105: OnMessageSubscriber sub = (OnMessageSubscriber) ClientPool
106: .get(this );
107: if (sub == null) {
108: sub = new OnMessageSubscriber(this
109: .getUseJNDIPropertiesAsBoolean(), this
110: .getJNDIInitialContextFactory(), this
111: .getProviderUrl(), this .getConnectionFactory(),
112: this .getTopic(), this .getUseAuth(), this
113: .getUsername(), this .getPassword());
114: sub.setMessageListener(this );
115: sub.resume();
116: ClientPool.addClient(sub);
117: ClientPool.put(this , sub);
118: log.info("SubscriberSampler.initListenerClient called");
119: log.info("loop count " + this .getIterations());
120: }
121: this .RUN = true;
122: return sub;
123: }
124:
125: /**
126: * Create the ReceiveSubscriber client for the sampler.
127: */
128: public void initReceiveClient() {
129: this .SUBSCRIBER = new ReceiveSubscriber(this
130: .getUseJNDIPropertiesAsBoolean(), this
131: .getJNDIInitialContextFactory(), this .getProviderUrl(),
132: this .getConnectionFactory(), this .getTopic(), this
133: .getUseAuth(), this .getUsername(), this
134: .getPassword());
135: this .SUBSCRIBER.resume();
136: ClientPool.addClient(this .SUBSCRIBER);
137: log.info("SubscriberSampler.initReceiveClient called");
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
144: */
145: public SampleResult sample(Entry e) {
146: return this .sample();
147: }
148:
149: /**
150: * sample method will check which client it should use and call the
151: * appropriate client specific sample method.
152: *
153: * @return the appropriate sample result
154: */
155: public SampleResult sample() {
156: if (this .getClientChoice().equals(JMSSubscriberGui.receive_str)) {
157: return sampleWithReceive();
158: } else {
159: return sampleWithListener();
160: }
161: }
162:
163: /**
164: * sample will block until messages are received
165: *
166: * @return the sample result
167: */
168: public SampleResult sampleWithListener() {
169: SampleResult result = new SampleResult();
170: result.setSampleLabel(getName());
171: OnMessageSubscriber sub = initListenerClient();
172:
173: this .loop = this .getIterationCount();
174:
175: result.sampleStart();
176: while (this .RUN && this .count(0) < this .loop) {
177: try {
178: Thread.sleep(0, 50);
179: } catch (Exception e) {
180: log.info(e.getMessage());
181: }
182: }
183: result.sampleEnd();
184: result.setResponseMessage(loop + " samples messages recieved");
185: if (this .getReadResponseAsBoolean()) {
186: result.setResponseData(this .BUFFER.toString().getBytes());
187: } else {
188: result.setBytes(this .BUFFER.toString().getBytes().length);
189: }
190: result.setSuccessful(true);
191: result.setResponseCode(loop
192: + " message(s) recieved successfully");
193: result.setSamplerData("Not applicable");
194: result.setSampleCount(loop);
195:
196: this .resetCount();
197: return result;
198: }
199:
200: /**
201: * Sample method uses the ReceiveSubscriber client instead of onMessage
202: * approach.
203: *
204: * @return the sample result
205: */
206: public SampleResult sampleWithReceive() {
207: SampleResult result = new SampleResult();
208: result.setSampleLabel(getName());
209: if (this .SUBSCRIBER == null) {
210: this .initReceiveClient();
211: this .SUBSCRIBER.start();
212: }
213: this .loop = this .getIterationCount();
214: this .SUBSCRIBER.setLoop(this .loop);
215:
216: result.sampleStart();
217: while (this .SUBSCRIBER.count(0) < this .loop) {
218: try {
219: Thread.sleep(0, 50);
220: } catch (Exception e) {
221: log.info(e.getMessage());
222: }
223: }
224: result.sampleEnd();
225: result.setResponseMessage(loop + " samples messages recieved");
226: if (this .getReadResponseAsBoolean()) {
227: result.setResponseData(this .SUBSCRIBER.getMessage()
228: .getBytes());
229: } else {
230: result
231: .setBytes(this .SUBSCRIBER.getMessage().getBytes().length);
232: }
233: result.setSuccessful(true);
234: result.setResponseCode(loop
235: + " message(s) recieved successfully");
236: result.setSamplerData("Not applicable");
237: result.setSampleCount(this .loop);
238:
239: this .SUBSCRIBER.clear();
240: this .SUBSCRIBER.resetCount();
241: return result;
242: }
243:
244: /**
245: * The sampler implements MessageListener directly and sets itself as the
246: * listener with the TopicSubscriber.
247: */
248: public synchronized void onMessage(Message message) {
249: try {
250: if (message instanceof TextMessage) {
251: TextMessage msg = (TextMessage) message;
252: String content = msg.getText();
253: if (content != null) {
254: this .BUFFER.append(content);
255: count(1);
256: }
257: }
258: } catch (JMSException e) {
259: log.error(e.getMessage());
260: }
261: }
262:
263: /**
264: * increment the count and return the new value.
265: *
266: * @param increment
267: * @return the new value
268: */
269: public synchronized int count(int increment) {
270: this .counter += increment;
271: return this .counter;
272: }
273:
274: /**
275: * resetCount will set the counter to zero and set the length of the
276: * StringBuffer to zero.
277: */
278: public synchronized void resetCount() {
279: this .counter = 0;
280: this .BUFFER.setLength(0);
281: }
282:
283: // ----------- get/set methods ------------------- //
284: /**
285: * Set the client choice. There are two options: ReceiveSusbscriber and
286: * OnMessageSubscriber.
287: */
288: public void setClientChoice(String choice) {
289: setProperty(CLIENT_CHOICE, choice);
290: }
291:
292: /**
293: * Return the client choice.
294: *
295: * @return the client choice
296: */
297: public String getClientChoice() {
298: return getPropertyAsString(CLIENT_CHOICE);
299: }
300: }
|