1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.mr.client.impl;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.net.MalformedURLException;
29 import java.net.URISyntaxException;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.LinkedList;
33 import java.util.List;
35 import java.util.Properties;
36 import java.util.concurrent.LinkedBlockingQueue;
37 import java.util.concurrent.ScheduledThreadPoolExecutor;
38 import java.util.concurrent.TimeUnit;
39 import java.util.zip.GZIPOutputStream;
41 import javax.ws.rs.core.MultivaluedMap;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import org.apache.http.HttpException;
47 import org.apache.http.HttpStatus;
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.json.JSONTokener;
52 import com.att.aft.dme2.api.DME2Client;
53 import com.att.aft.dme2.api.DME2Exception;
54 import com.att.nsa.mr.client.HostSelector;
55 import com.att.nsa.mr.client.MRBatchingPublisher;
56 import com.att.nsa.mr.client.response.MRPublisherResponse;
57 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
59 public class MRSimplerBatchPublisher extends MRBaseClient implements MRBatchingPublisher {
60 private static final Logger logger = LoggerFactory.getLogger(MRSimplerBatchPublisher.class);
62 public static class Builder {
66 public Builder againstUrls(Collection<String> baseUrls) {
71 public Builder onTopic(String topic) {
76 public Builder batchTo(int maxBatchSize, long maxBatchAgeMs) {
77 fMaxBatchSize = maxBatchSize;
78 fMaxBatchAgeMs = maxBatchAgeMs;
82 public Builder compress(boolean compress) {
87 public Builder httpThreadTime(int threadOccuranceTime) {
88 this.threadOccuranceTime = threadOccuranceTime;
92 public Builder allowSelfSignedCertificates(boolean allowSelfSignedCerts) {
93 fAllowSelfSignedCerts = allowSelfSignedCerts;
97 public Builder withResponse(boolean withResponse) {
98 fWithResponse = withResponse;
102 public MRSimplerBatchPublisher build() {
103 if (!fWithResponse) {
105 return new MRSimplerBatchPublisher(fUrls, fTopic, fMaxBatchSize, fMaxBatchAgeMs, fCompress,
106 fAllowSelfSignedCerts, threadOccuranceTime);
107 } catch (MalformedURLException e) {
108 throw new IllegalArgumentException(e);
112 return new MRSimplerBatchPublisher(fUrls, fTopic, fMaxBatchSize, fMaxBatchAgeMs, fCompress,
113 fAllowSelfSignedCerts, fMaxBatchSize);
114 } catch (MalformedURLException e) {
115 throw new IllegalArgumentException(e);
121 private Collection<String> fUrls;
122 private String fTopic;
123 private int fMaxBatchSize = 100;
124 private long fMaxBatchAgeMs = 1000;
125 private boolean fCompress = false;
126 private int threadOccuranceTime = 50;
127 private boolean fAllowSelfSignedCerts = false;
128 private boolean fWithResponse = false;
133 public int send(String partition, String msg) {
134 return send(new message(partition, msg));
138 public int send(String msg) {
139 return send(new message(null, msg));
143 public int send(message msg) {
144 final LinkedList<message> list = new LinkedList<message>();
150 public synchronized int send(Collection<message> msgs) {
152 throw new IllegalStateException("The publisher was closed.");
155 for (message userMsg : msgs) {
156 fPending.add(new TimestampedMessage(userMsg));
158 return getPendingMessageCount();
162 public synchronized int getPendingMessageCount() {
163 return fPending.size();
167 public void close() {
169 final List<message> remains = close(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
170 if (remains.size() > 0) {
171 getLog().warn("Closing publisher with " + remains.size() + " messages unsent. "
172 + "Consider using MRBatchingPublisher.close( long timeout, TimeUnit timeoutUnits ) to recapture unsent messages on close.");
174 } catch (InterruptedException e) {
175 getLog().warn("Possible message loss. " + e.getMessage(), e);
176 } catch (IOException e) {
177 getLog().warn("Possible message loss. " + e.getMessage(), e);
182 public List<message> close(long time, TimeUnit unit) throws IOException, InterruptedException {
183 synchronized (this) {
186 // stop the background sender
187 fExec.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
188 fExec.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
192 final long now = Clock.now();
193 final long waitInMs = TimeUnit.MILLISECONDS.convert(time, unit);
194 final long timeoutAtMs = now + waitInMs;
196 while (Clock.now() < timeoutAtMs && getPendingMessageCount() > 0) {
201 synchronized (this) {
202 final LinkedList<message> result = new LinkedList<message>();
203 fPending.drainTo(result);
209 * Possibly send a batch to the MR server. This is called by the background
210 * thread and the close() method
214 private synchronized void send(boolean force) {
215 if (force || shouldSendNow()) {
217 getLog().warn("Send failed, " + fPending.size() + " message to send.");
219 // note the time for back-off
220 fDontSendUntilMs = sfWaitAfterError + Clock.now();
225 private synchronized boolean shouldSendNow() {
226 boolean shouldSend = false;
227 if (fPending.size() > 0) {
228 final long nowMs = Clock.now();
230 shouldSend = (fPending.size() >= fMaxBatchSize);
232 final long sendAtMs = fPending.peek().timestamp + fMaxBatchAgeMs;
233 shouldSend = sendAtMs <= nowMs;
236 // however, wait after an error
237 shouldSend = shouldSend && nowMs >= fDontSendUntilMs;
243 * Method to parse published JSON Objects and Arrays
247 private JSONArray parseJSON() {
248 JSONArray jsonArray = new JSONArray();
249 for (TimestampedMessage m : fPending) {
250 JSONTokener jsonTokener = new JSONTokener(m.fMsg);
251 JSONObject jsonObject = null;
252 JSONArray tempjsonArray = null;
253 final char firstChar = jsonTokener.next();
255 if ('[' == firstChar) {
256 tempjsonArray = new JSONArray(jsonTokener);
257 if (null != tempjsonArray) {
258 for (int i = 0; i < tempjsonArray.length(); i++) {
259 jsonArray.put(tempjsonArray.getJSONObject(i));
263 jsonObject = new JSONObject(jsonTokener);
264 jsonArray.put(jsonObject);
271 private synchronized boolean sendBatch() {
272 // it's possible for this call to be made with an empty list. in this
273 // case, just return.
274 if (fPending.size() < 1) {
278 final long nowMs = Clock.now();
280 host = this.fHostSelector.selectBaseHost();
282 final String httpurl = MRConstants.makeUrl(host, fTopic, props.getProperty("Protocol"),
283 props.getProperty("partition"));
287 * final String contentType = fCompress ?
288 * MRFormat.CAMBRIA_ZIP.toString () : MRFormat.CAMBRIA.toString () ;
291 final ByteArrayOutputStream baseStream = new ByteArrayOutputStream();
292 OutputStream os = baseStream;
293 final String contentType = props.getProperty("contenttype");
294 if (contentType.equalsIgnoreCase("application/json")) {
295 JSONArray jsonArray = parseJSON();
296 os.write(jsonArray.toString().getBytes());
299 } else if (contentType.equalsIgnoreCase("text/plain")) {
300 for (TimestampedMessage m : fPending) {
301 os.write(m.fMsg.getBytes());
305 } else if (contentType.equalsIgnoreCase("application/cambria")
306 || (contentType.equalsIgnoreCase("application/cambria-zip"))) {
307 if (contentType.equalsIgnoreCase("application/cambria-zip")) {
308 os = new GZIPOutputStream(baseStream);
310 for (TimestampedMessage m : fPending) {
312 os.write(("" + m.fPartition.length()).getBytes());
314 os.write(("" + m.fMsg.length()).getBytes());
316 os.write(m.fPartition.getBytes());
317 os.write(m.fMsg.getBytes());
322 for (TimestampedMessage m : fPending) {
323 os.write(m.fMsg.getBytes());
329 final long startMs = Clock.now();
330 if (ProtocolTypeConstants.DME2.getValue().equalsIgnoreCase(protocolFlag)) {
335 getLog().info("sending " + fPending.size() + " msgs to " + url + subContextPath + ". Oldest: "
336 + (nowMs - fPending.peek().timestamp) + " ms");
337 sender.setPayload(os.toString());
338 String dmeResponse = sender.sendAndWait(5000L);
340 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + dmeResponse.toString();
341 getLog().info(logLine);
346 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
347 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
348 + (nowMs - fPending.peek().timestamp) + " ms");
349 final JSONObject result = postAuth(httpurl, baseStream.toByteArray(), contentType, authKey, authDate,
350 username, password, protocolFlag);
351 // Here we are checking for error response. If HTTP status
352 // code is not within the http success response code
353 // then we consider this as error and return false
354 if (result.getInt("status") < 200 || result.getInt("status") > 299) {
357 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
358 getLog().info(logLine);
363 if (ProtocolTypeConstants.AAF_AUTH.getValue().equalsIgnoreCase(protocolFlag)) {
364 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
365 + (nowMs - fPending.peek().timestamp) + " ms");
366 final JSONObject result = post(httpurl, baseStream.toByteArray(), contentType, username, password,
369 // Here we are checking for error response. If HTTP status
370 // code is not within the http success response code
371 // then we consider this as error and return false
372 if (result.getInt("status") < 200 || result.getInt("status") > 299) {
375 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
376 getLog().info(logLine);
380 } catch (IllegalArgumentException x) {
381 getLog().warn(x.getMessage(), x);
382 } catch (IOException x) {
383 getLog().warn(x.getMessage(), x);
384 } catch (HttpException x) {
385 getLog().warn(x.getMessage(), x);
386 } catch (Exception x) {
387 getLog().warn(x.getMessage(), x);
392 public synchronized MRPublisherResponse sendBatchWithResponse() {
393 // it's possible for this call to be made with an empty list. in this
394 // case, just return.
395 if (fPending.size() < 1) {
396 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
397 pubResponse.setResponseMessage("No Messages to send");
401 final long nowMs = Clock.now();
403 host = this.fHostSelector.selectBaseHost();
405 final String httpurl = MRConstants.makeUrl(host, fTopic, props.getProperty("Protocol"),
406 props.getProperty("partition"));
407 OutputStream os = null;
410 final ByteArrayOutputStream baseStream = new ByteArrayOutputStream();
412 final String contentType = props.getProperty("contenttype");
413 if (contentType.equalsIgnoreCase("application/json")) {
414 JSONArray jsonArray = parseJSON();
415 os.write(jsonArray.toString().getBytes());
416 } else if (contentType.equalsIgnoreCase("text/plain")) {
417 for (TimestampedMessage m : fPending) {
418 os.write(m.fMsg.getBytes());
421 } else if (contentType.equalsIgnoreCase("application/cambria")
422 || (contentType.equalsIgnoreCase("application/cambria-zip"))) {
423 if (contentType.equalsIgnoreCase("application/cambria-zip")) {
424 os = new GZIPOutputStream(baseStream);
426 for (TimestampedMessage m : fPending) {
428 os.write(("" + m.fPartition.length()).getBytes());
430 os.write(("" + m.fMsg.length()).getBytes());
432 os.write(m.fPartition.getBytes());
433 os.write(m.fMsg.getBytes());
438 for (TimestampedMessage m : fPending) {
439 os.write(m.fMsg.getBytes());
444 final long startMs = Clock.now();
445 if (ProtocolTypeConstants.DME2.getValue().equalsIgnoreCase(protocolFlag)) {
451 getLog().info("sending " + fPending.size() + " msgs to " + url + subContextPath + ". Oldest: "
452 + (nowMs - fPending.peek().timestamp) + " ms");
453 sender.setPayload(os.toString());
455 String dmeResponse = sender.sendAndWait(5000L);
457 pubResponse = createMRPublisherResponse(dmeResponse, pubResponse);
459 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
460 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
464 final String logLine = String.valueOf((Clock.now() - startMs)) + dmeResponse.toString();
465 getLog().info(logLine);
468 } catch (DME2Exception x) {
469 getLog().warn(x.getMessage(), x);
470 pubResponse.setResponseCode(x.getErrorCode());
471 pubResponse.setResponseMessage(x.getErrorMessage());
472 } catch (URISyntaxException x) {
474 getLog().warn(x.getMessage(), x);
475 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
476 pubResponse.setResponseMessage(x.getMessage());
477 } catch (Exception x) {
479 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
480 pubResponse.setResponseMessage(x.getMessage());
481 logger.error("exception: ", x);
488 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
489 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
490 + (nowMs - fPending.peek().timestamp) + " ms");
491 final String result = postAuthwithResponse(httpurl, baseStream.toByteArray(), contentType, authKey,
492 authDate, username, password, protocolFlag);
493 // Here we are checking for error response. If HTTP status
494 // code is not within the http success response code
495 // then we consider this as error and return false
497 pubResponse = createMRPublisherResponse(result, pubResponse);
499 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
500 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
505 final String logLine = "MR reply ok (" + (Clock.now() - startMs) + " ms):" + result.toString();
506 getLog().info(logLine);
511 if (ProtocolTypeConstants.AAF_AUTH.getValue().equalsIgnoreCase(protocolFlag)) {
512 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
513 + (nowMs - fPending.peek().timestamp) + " ms");
514 final String result = postWithResponse(httpurl, baseStream.toByteArray(), contentType, username,
515 password, protocolFlag);
517 // Here we are checking for error response. If HTTP status
518 // code is not within the http success response code
519 // then we consider this as error and return false
520 pubResponse = createMRPublisherResponse(result, pubResponse);
522 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
523 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
528 final String logLine = String.valueOf((Clock.now() - startMs));
529 getLog().info(logLine);
534 if (ProtocolTypeConstants.HTTPNOAUTH.getValue().equalsIgnoreCase(protocolFlag)) {
535 getLog().info("sending " + fPending.size() + " msgs to " + httpurl + ". Oldest: "
536 + (nowMs - fPending.peek().timestamp) + " ms");
537 final String result = postNoAuthWithResponse(httpurl, baseStream.toByteArray(), contentType);
539 // Here we are checking for error response. If HTTP status
540 // code is not within the http success response code
541 // then we consider this as error and return false
542 pubResponse = createMRPublisherResponse(result, pubResponse);
544 if (Integer.valueOf(pubResponse.getResponseCode()) < 200
545 || Integer.valueOf(pubResponse.getResponseCode()) > 299) {
550 final String logLine = String.valueOf((Clock.now() - startMs));
551 getLog().info(logLine);
555 } catch (IllegalArgumentException x) {
556 getLog().warn(x.getMessage(), x);
557 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
558 pubResponse.setResponseMessage(x.getMessage());
560 } catch (IOException x) {
561 getLog().warn(x.getMessage(), x);
562 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
563 pubResponse.setResponseMessage(x.getMessage());
565 } catch (HttpException x) {
566 getLog().warn(x.getMessage(), x);
567 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
568 pubResponse.setResponseMessage(x.getMessage());
570 } catch (Exception x) {
571 getLog().warn(x.getMessage(), x);
573 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
574 pubResponse.setResponseMessage(x.getMessage());
579 if (fPending.size() > 0) {
580 getLog().warn("Send failed, " + fPending.size() + " message to send.");
581 pubResponse.setPendingMsgs(fPending.size());
586 } catch (Exception x) {
587 getLog().warn(x.getMessage(), x);
588 pubResponse.setResponseCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
589 pubResponse.setResponseMessage("Error in closing Output Stream");
597 public MRPublisherResponse createMRPublisherResponse(String reply, MRPublisherResponse mrPubResponse) {
599 if (reply.isEmpty()) {
601 mrPubResponse.setResponseCode(String.valueOf(HttpStatus.SC_BAD_REQUEST));
602 mrPubResponse.setResponseMessage("Please verify the Producer properties");
603 } else if (reply.startsWith("{")) {
604 JSONObject jObject = new JSONObject(reply);
605 if (jObject.has("message") && jObject.has("status")) {
606 String message = jObject.getString("message");
607 if (null != message) {
608 mrPubResponse.setResponseMessage(message);
610 mrPubResponse.setResponseCode(Integer.toString(jObject.getInt("status")));
612 mrPubResponse.setResponseCode(String.valueOf(HttpStatus.SC_OK));
613 mrPubResponse.setResponseMessage(reply);
615 } else if (reply.startsWith("<")) {
616 String responseCode = getHTTPErrorResponseCode(reply);
617 if (responseCode.contains("403")) {
618 responseCode = "403";
620 mrPubResponse.setResponseCode(responseCode);
621 mrPubResponse.setResponseMessage(getHTTPErrorResponseMessage(reply));
624 return mrPubResponse;
627 private final String fTopic;
628 private final int fMaxBatchSize;
629 private final long fMaxBatchAgeMs;
630 private final boolean fCompress;
631 private int threadOccuranceTime;
632 private boolean fClosed;
633 private String username;
634 private String password;
638 private HostSelector fHostSelector = null;
640 private final LinkedBlockingQueue<TimestampedMessage> fPending;
641 private long fDontSendUntilMs;
642 private final ScheduledThreadPoolExecutor fExec;
644 private String latitude;
645 private String longitude;
646 private String version;
647 private String serviceName;
649 private String partner;
650 private String routeOffer;
651 private String subContextPath;
652 private String protocol;
653 private String methodType;
655 private String dmeuser;
656 private String dmepassword;
657 private String contentType;
658 private static final long sfWaitAfterError = 10000;
659 private HashMap<String, String> DMETimeOuts;
660 private DME2Client sender;
661 public String protocolFlag = ProtocolTypeConstants.DME2.getValue();
662 private String authKey;
663 private String authDate;
664 private String handlers;
665 private Properties props;
666 public static String routerFilePath;
667 protected static final Map<String, String> headers = new HashMap<String, String>();
668 public static MultivaluedMap<String, Object> headersMap;
670 private MRPublisherResponse pubResponse;
672 public MRPublisherResponse getPubResponse() {
676 public void setPubResponse(MRPublisherResponse pubResponse) {
677 this.pubResponse = pubResponse;
680 public static String getRouterFilePath() {
681 return routerFilePath;
684 public static void setRouterFilePath(String routerFilePath) {
685 MRSimplerBatchPublisher.routerFilePath = routerFilePath;
688 public Properties getProps() {
692 public void setProps(Properties props) {
696 public String getProtocolFlag() {
700 public void setProtocolFlag(String protocolFlag) {
701 this.protocolFlag = protocolFlag;
704 private void DME2Configue() throws Exception {
708 * FileReader reader = new FileReader(new File (producerFilePath));
709 * Properties props = new Properties(); props.load(reader);
711 latitude = props.getProperty("Latitude");
712 longitude = props.getProperty("Longitude");
713 version = props.getProperty("Version");
714 serviceName = props.getProperty("ServiceName");
715 env = props.getProperty("Environment");
716 partner = props.getProperty("Partner");
717 routeOffer = props.getProperty("routeOffer");
718 subContextPath = props.getProperty("SubContextPath") + fTopic;
720 * if(props.getProperty("partition")!=null &&
721 * !props.getProperty("partition").equalsIgnoreCase("")){
722 * subContextPath=subContextPath+"?partitionKey="+props.getProperty(
725 protocol = props.getProperty("Protocol");
726 methodType = props.getProperty("MethodType");
727 dmeuser = props.getProperty("username");
728 dmepassword = props.getProperty("password");
729 contentType = props.getProperty("contenttype");
730 handlers = props.getProperty("sessionstickinessrequired");
731 routerFilePath = props.getProperty("DME2preferredRouterFilePath");
734 * Changes to DME2Client url to use Partner for auto failover
735 * between data centers When Partner value is not provided use the
736 * routeOffer value for auto failover within a cluster
739 String partitionKey = props.getProperty("partition");
741 if (partner != null && !partner.isEmpty()) {
742 url = protocol + "://" + serviceName + "?version=" + version + "&envContext=" + env + "&partner="
744 if (partitionKey != null && !partitionKey.equalsIgnoreCase("")) {
745 url = url + "&partitionKey=" + partitionKey;
747 } else if (routeOffer != null && !routeOffer.isEmpty()) {
748 url = protocol + "://" + serviceName + "?version=" + version + "&envContext=" + env + "&routeoffer="
750 if (partitionKey != null && !partitionKey.equalsIgnoreCase("")) {
751 url = url + "&partitionKey=" + partitionKey;
755 DMETimeOuts = new HashMap<String, String>();
756 DMETimeOuts.put("AFT_DME2_EP_READ_TIMEOUT_MS", props.getProperty("AFT_DME2_EP_READ_TIMEOUT_MS"));
757 DMETimeOuts.put("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", props.getProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS"));
758 DMETimeOuts.put("AFT_DME2_EP_CONN_TIMEOUT", props.getProperty("AFT_DME2_EP_CONN_TIMEOUT"));
759 DMETimeOuts.put("Content-Type", contentType);
760 System.setProperty("AFT_LATITUDE", latitude);
761 System.setProperty("AFT_LONGITUDE", longitude);
762 System.setProperty("AFT_ENVIRONMENT", props.getProperty("AFT_ENVIRONMENT"));
763 // System.setProperty("DME2.DEBUG", "true");
766 // System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS",
767 // "SSLv3,TLSv1,TLSv1.1");
768 System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "TLSv1.1,TLSv1.2");
769 System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
770 System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
774 sender = new DME2Client(new URI(url), 5000L);
776 sender.setAllowAllHttpReturnCodes(true);
777 sender.setMethod(methodType);
778 sender.setSubContext(subContextPath);
779 sender.setCredentials(dmeuser, dmepassword);
780 sender.setHeaders(DMETimeOuts);
781 if (handlers.equalsIgnoreCase("yes")) {
782 sender.addHeader("AFT_DME2_EXCHANGE_REQUEST_HANDLERS",
783 props.getProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS"));
784 sender.addHeader("AFT_DME2_EXCHANGE_REPLY_HANDLERS",
785 props.getProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS"));
786 sender.addHeader("AFT_DME2_REQ_TRACE_ON", props.getProperty("AFT_DME2_REQ_TRACE_ON"));
788 sender.addHeader("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "com.att.nsa.mr.dme.client.HeaderReplyHandler");
790 } catch (DME2Exception x) {
791 getLog().warn(x.getMessage(), x);
792 throw new DME2Exception(x.getErrorCode(), x.getErrorMessage());
793 } catch (URISyntaxException x) {
795 getLog().warn(x.getMessage(), x);
796 throw new URISyntaxException(url, x.getMessage());
797 } catch (Exception x) {
799 getLog().warn(x.getMessage(), x);
800 throw new IllegalArgumentException(x.getMessage());
804 private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
805 boolean compress) throws MalformedURLException {
808 if (topic == null || topic.length() < 1) {
809 throw new IllegalArgumentException("A topic must be provided.");
812 fHostSelector = new HostSelector(hosts, null);
815 fMaxBatchSize = maxBatchSize;
816 fMaxBatchAgeMs = maxBatchAgeMs;
817 fCompress = compress;
819 fPending = new LinkedBlockingQueue<TimestampedMessage>();
820 fDontSendUntilMs = 0;
821 fExec = new ScheduledThreadPoolExecutor(1);
822 pubResponse = new MRPublisherResponse();
826 private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
827 boolean compress, boolean allowSelfSignedCerts, int httpThreadOccurnace) throws MalformedURLException {
830 if (topic == null || topic.length() < 1) {
831 throw new IllegalArgumentException("A topic must be provided.");
834 fHostSelector = new HostSelector(hosts, null);
837 fMaxBatchSize = maxBatchSize;
838 fMaxBatchAgeMs = maxBatchAgeMs;
839 fCompress = compress;
840 threadOccuranceTime = httpThreadOccurnace;
841 fPending = new LinkedBlockingQueue<TimestampedMessage>();
842 fDontSendUntilMs = 0;
843 fExec = new ScheduledThreadPoolExecutor(1);
844 fExec.scheduleAtFixedRate(new Runnable() {
849 }, 100, threadOccuranceTime, TimeUnit.MILLISECONDS);
852 private static class TimestampedMessage extends message {
853 public TimestampedMessage(message m) {
855 timestamp = Clock.now();
858 public final long timestamp;
861 public String getUsername() {
865 public void setUsername(String username) {
866 this.username = username;
869 public String getPassword() {
873 public void setPassword(String password) {
874 this.password = password;
877 public String getHost() {
881 public void setHost(String host) {
885 public String getContentType() {
889 public void setContentType(String contentType) {
890 this.contentType = contentType;
893 public String getAuthKey() {
897 public void setAuthKey(String authKey) {
898 this.authKey = authKey;
901 public String getAuthDate() {
905 public void setAuthDate(String authDate) {
906 this.authDate = authDate;