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 org.onap.dmaap.mr.tools;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.att.nsa.cmdtool.Command;
33 import com.att.nsa.cmdtool.CommandNotReadyException;
34 import org.onap.dmaap.mr.client.MRBatchingPublisher;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRConsumer;
37 import org.onap.dmaap.mr.client.MRClientBuilders.PublisherBuilder;
38 import org.onap.dmaap.mr.client.MRPublisher.message;
40 public class MessageCommand implements Command<MRCommandContext>
42 final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
44 public String[] getMatches ()
47 "(post) (\\S*) (\\S*) (.*)",
48 "(read) (\\S*) (\\S*) (\\S*)",
53 public void checkReady ( MRCommandContext context ) throws CommandNotReadyException
55 if ( !context.checkClusterReady () )
57 throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." );
62 public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException
64 if ( parts[0].equalsIgnoreCase ( "read" ))
66 final MRConsumer cc = MRClientFactory.createConsumer ( context.getCluster (), parts[1], parts[2], parts[3],
67 -1, -1, null, context.getApiKey(), context.getApiPwd() );
68 context.applyTracer ( cc );
71 for ( String msg : cc.fetch () )
78 out.println ( "Problem fetching messages: " + e.getMessage() );
79 logger.error("Problem fetching messages: ", e);
88 final MRBatchingPublisher pub=ToolsUtil.createBatchPublisher(context, parts[1]);
91 pub.send ( parts[2], parts[3] );
93 catch ( IOException e )
95 out.println ( "Problem sending message: " + e.getMessage() );
96 logger.error("Problem sending message: ", e);
100 List<message> left = null;
103 left = pub.close ( 500, TimeUnit.MILLISECONDS );
105 catch ( IOException e )
107 out.println ( "Problem sending message: " + e.getMessage() );
108 logger.error("Problem sending message: ", e);
110 catch ( InterruptedException e )
112 out.println ( "Problem sending message: " + e.getMessage() );
113 logger.error("Problem sending message: ", e);
114 Thread.currentThread().interrupt();
116 if ( left != null && left.isEmpty() )
118 out.println ( left.size() + " messages not sent." );
125 public void displayHelp ( PrintStream out )
127 out.println ( "post <topicName> <partition> <message>" );
128 out.println ( "read <topicName> <consumerGroup> <consumerId>" );