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.MRPublisher.message;
39 public class MessageCommand implements Command<MRCommandContext>
41 final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
43 public String[] getMatches ()
46 "(post) (\\S*) (\\S*) (.*)",
47 "(read) (\\S*) (\\S*) (\\S*)",
52 public void checkReady ( MRCommandContext context ) throws CommandNotReadyException
54 if ( !context.checkClusterReady () )
56 throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." );
61 public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException
63 if ( parts[0].equalsIgnoreCase ( "read" ))
65 final MRConsumer cc = MRClientFactory.createConsumer ( context.getCluster (), parts[1], parts[2], parts[3],
66 -1, -1, null, context.getApiKey(), context.getApiPwd() );
67 context.applyTracer ( cc );
70 for ( String msg : cc.fetch () )
77 out.println ( "Problem fetching messages: " + e.getMessage() );
78 logger.error("Problem fetching messages: ", e);
87 final MRBatchingPublisher pub=ToolsUtil.createBatchPublisher(context, parts[1]);
90 pub.send ( parts[2], parts[3] );
92 catch ( IOException e )
94 out.println ( "Problem sending message: " + e.getMessage() );
95 logger.error("Problem sending message: ", e);
99 List<message> left = null;
102 left = pub.close ( 500, TimeUnit.MILLISECONDS );
104 catch ( IOException e )
106 out.println ( "Problem sending message: " + e.getMessage() );
107 logger.error("Problem sending message: ", e);
109 catch ( InterruptedException e )
111 out.println ( "Problem sending message: " + e.getMessage() );
112 logger.error("Problem sending message: ", e);
113 Thread.currentThread().interrupt();
115 if ( left != null && left.isEmpty() )
117 out.println ( left.size() + " messages not sent." );
124 public void displayHelp ( PrintStream out )
126 out.println ( "post <topicName> <partition> <message>" );
127 out.println ( "read <topicName> <consumerGroup> <consumerId>" );