Following is the code which I used to publish the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | package org.wso2.carbon.publisher; import org.apache.log4j.Logger; import org.wso2.carbon.databridge.agent.thrift.Agent; import org.wso2.carbon.databridge.agent.thrift.DataPublisher; import org.wso2.carbon.databridge.agent.thrift.conf.AgentConfiguration; import org.wso2.carbon.databridge.agent.thrift.exception.AgentException; import org.wso2.carbon.databridge.commons.Event; import org.wso2.carbon.databridge.commons.exception.*; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.SocketException; /** * BAMDataPublisherExample.java */ public class BAMDataPublisherExample { private Logger log = Logger.getLogger(BAMDataPublisherExample.class); private DataPublisher dataPublisher = null; public BAMDataPublisherExample() { initDataPublisher(); } /** * Initialize the BAM data publisher */ private void initDataPublisher() { try { log.info("Starting BAM client\n"); AgentConfiguration agentConfiguration = new AgentConfiguration(); System.setProperty("javax.net.ssl.trustStore", "/home/aruna/wso2bam/repository/resources/security/client-truststore.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); Agent agent = new Agent(agentConfiguration); String host; host = "localhost"; String port = "7611"; String username = "admin"; String password = "admin"; this.dataPublisher = new DataPublisher("tcp://" + host + ":" + port, username, password, agent); } catch (AgentException e) { log.error("Exception", e); } catch (MalformedURLException e) { log.error("Exception", e); } catch (org.wso2.carbon.databridge.commons.exception.AuthenticationException e) { log.error("Exception", e); } catch (TransportException e) { log.error("Exception", e); } } /** * @param dataPublisher * @param streamId * @param dataArray * @throws org.wso2.carbon.databridge.agent.thrift.exception.AgentException */ private void publishEvents(DataPublisher dataPublisher, String streamId, String dataArray[]) throws AgentException { Object[] objectArray = new Object[dataArray.length]; for (int i = 0; i < dataArray.length; i++) { objectArray[i] = dataArray[i]; } Event event = new Event(streamId, System.currentTimeMillis(), null, null, objectArray); this.dataPublisher.publish(event); } /** * Stop the publisher */ public void stopPublisher() { log.info("Stopping Data Publisher"); this.dataPublisher.stop(); } /** * @param dataArray * @param streamName * @param version * @throws org.wso2.carbon.databridge.commons.exception.NoStreamDefinitionExistException * @throws java.net.SocketException * @throws java.io.UnsupportedEncodingException * @throws org.wso2.carbon.databridge.agent.thrift.exception.AgentException * @throws org.wso2.carbon.databridge.commons.exception.MalformedStreamDefinitionException * @throws org.wso2.carbon.databridge.commons.exception.StreamDefinitionException * @throws org.wso2.carbon.databridge.commons.exception.DifferentStreamDefinitionAlreadyDefinedException */ public void repositoryDataPublish(String[] dataArray, String streamName, String version) throws NoStreamDefinitionExistException, SocketException, UnsupportedEncodingException, AgentException, MalformedStreamDefinitionException, StreamDefinitionException, DifferentStreamDefinitionAlreadyDefinedException { String streamId = null; try { streamId = this.dataPublisher.findStream(streamName, version); } catch (NoStreamDefinitionExistException e) { String[] fields = {"repositoryID", "ownerID", "totalNumberOfCommits", "topContributor", "totalPullRequests", "lastCommitDateTime", "commitsThisMonth", "commitLastYear"}; String[] types = {"STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING"}; streamId = dataPublisher.defineStream(getTableString(fields, types, streamName, version, "Repository Data", "Repository Statistics Data")); } catch (Exception ex) { log.error("***Exception****" + ex); } // Publish event for a valid stream if (!streamId.isEmpty()) { log.info("Stream ID: " + streamId + "\n"); publishEvents(dataPublisher, streamId, dataArray); } } /** * Create the Bam Table creation String * * @param fields * @param types * @param streamName * @param version * @param nickname * @param descrption * @return */ public String getTableString(String[] fields, String[] types, String streamName, String version , String nickname, String descrption) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("{ 'name':'"); stringBuilder.append(streamName); stringBuilder.append("', 'version':'"); stringBuilder.append(version); stringBuilder.append("', 'nickName': '"); stringBuilder.append(nickname); stringBuilder.append("', 'description': '"); stringBuilder.append(descrption); stringBuilder.append("', 'payloadData':["); for (int i = 0; i < fields.length; i++) { stringBuilder.append("{'name':'"); stringBuilder.append(fields[i]); stringBuilder.append("','type':'"); stringBuilder.append(types[i]); if (i == fields.length - 1) { stringBuilder.append("'}"); } else { stringBuilder.append("'},"); } } stringBuilder.append(" ]}"); return stringBuilder.toString(); } public static void main(String[] args) { BAMDataPublisherExample pub = new BAMDataPublisherExample(); try { String data[] = {"carbon-kernel", "wso2", "302", "Kishanthan", "0", "2014-04-04", "14", "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0," + " 0, 6, 5, 20, 30, 2, 7, 3, 5, 14, 16, 20, 12, 54, 68, 6, 10, 19, 43]"}; pub.repositoryDataPublish(data, "GitHub_Stream", "1.0.0"); } catch (Exception e) { e.printStackTrace(); } finally { pub.stopPublisher(); } } } |
Remember the following when using the code.
- Set your BAM home location where you extracted the BAM pointing to the client-truststore.jks file. for example "/home/aruna/wso2bam/repository/resources/security/client-truststore.jks"
- Set the correct port for publish the data. If you have set an offset change the port accordingly.
- Set the correct username and password for publish data.
Loging to the BAM Admin Console and you'll see the published data as follows.
Admin Console View of the Published Data |
Visit the following link to See how to explore the WSO2 BAM Cluster and other BAM related documentation.
https://docs.wso2.org/display/BAM240/WSO2+Business+Activity+Monitor+Documentation