Skip to content

Commit e288115

Browse files
gfoster1mimaison
authored andcommitted
Add spring examples to readme + refactored the Liberty sample
1 parent 7aae795 commit e288115

File tree

6 files changed

+112
-176
lines changed

6 files changed

+112
-176
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ rest-nodejs-express-sample/node_modules
33
*.classpath
44
.project
55
**.DS_Store
6+
7+
*.iml
8+
.idea
9+
gradle*

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# event-streams-samples
2-
IBM Event Streams for IBM Cloud is a scalable, distributed, high throughput message bus to unite your on-premise and off-premise cloud technologies. You can wire micro-services together using open protocols, connect stream data to analytics to realise powerful insight and feed event data to multiple applications to react in real time.
2+
IBM Event Streams for IBM Cloud is a scalable, distributed, high throughput message bus to unite your on-premise and off-premise cloud technologies. You can wire micro-services together using open protocols, connect stream data to analytics to realize powerful insight and feed event data to multiple applications to react in real time.
33

44
This repository is for samples which interact with the Event Streams for IBM Cloud service.
55
Currently, there are samples for the Kafka and MQ Light APIs.
@@ -11,13 +11,11 @@ The aim of the samples is to help you get started with Event Streams for IBM Clo
1111
## Provisioning your Event Streams for IBM Cloud Cluster
1212
In order to provision an Event Streams for IBM Cloud cluster, please visit the [IBM Cloud® catalog](https://cloud.ibm.com/catalog/). Please also familiarise yourself with Event Streams for IBM Cloud and Apache Kafka basics and terminology. [Our documentation](https://cloud.ibm.com/docs/services/EventStreams?topic=eventstreams-getting_started) is a good starting point.
1313

14-
1514
### Pricing plans
1615
IBM Event Streams can be provisioned on IBM Cloud® in various pricing plans. Please refer to our [documentation](https://cloud.ibm.com/docs/services/EventStreams?topic=eventstreams-plan_choose#plan_choose) to help choose a plan that works for you.
1716

1817
__Important Note__: Provisioning an Event Streams service in IBM Cloud® incurs a fee. Please review pricing before provisioning. The samples in this repository will create topic(s) on your behalf - creating a topic might also incur a fee. For more information, please consult the IBM Cloud® documentation if necessary.
1918

20-
2119
## Connecting to your Event Streams for IBM Cloud Cluster
2220
In each sample, we demonstrate a single connection path for both our Classic and Standard/Enterprise plans respectively. The aim was to get you started quickly. However your client's needs might be different. Therefore we wrote a [guide](https://cloud.ibm.com/docs/services/EventStreams?topic=eventstreams-connecting#connecting) that discusses credential generation in detail and showing you all possible ways of doing this.
2321

@@ -31,6 +29,10 @@ In each sample, we demonstrate a single connection path for both our Classic and
3129
* [kafka-connect](/kafka-connect/README.md) : Sample Docker image with Kafka Connect
3230
* [kafka-mirrormaker](/kafka-mirrormaker/README.md) : Sample Docker image with Kafka Mirror Maker
3331

32+
### Spring Kafka:
33+
* [spring kafka tutorial](https://developer.ibm.com/tutorials/use-spring-kafka-to-access-an-event-streams-service/) : Tutorial to quickly get you up and running using IBM Event Streams.
34+
* [spring-kafka](https://github.com/wkorando/event-stream-kafka) : Sample app to connect to Event Streams using Spring Kafka
35+
3436
## Get Further Assistance
3537

3638
If you have any issues, just ask us a question (tagged with `ibm-eventstreams`) on [StackOverflow.com](http://stackoverflow.com/questions/tagged/ibm-eventstreams).

kafka-java-liberty-sample/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
/.gradle/
55
/.settings/
66
/.project
7+
8+
#### intellij settings
9+
*.iml
10+
.idea
11+
gradle*

kafka-java-liberty-sample/src/main/java/com/eventstreams/samples/env/Environment.java

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,55 @@
1818
* (c) Copyright IBM Corp. 2018
1919
*/
2020
package com.eventstreams.samples.env;
21-
import java.io.IOException;
22-
import java.util.Iterator;
2321

24-
import org.apache.kafka.common.errors.IllegalSaslStateException;
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
2524
import org.apache.log4j.Level;
2625
import org.apache.log4j.Logger;
2726

28-
import com.fasterxml.jackson.databind.JsonNode;
29-
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import java.io.IOException;
28+
import java.util.Iterator;
3029

3130
public class Environment {
3231

3332
private static final Logger logger = Logger.getLogger(Environment.class);
34-
private static final String SERVICE_NAME = "messagehub";
3533

3634
public static EventStreamsCredentials getEventStreamsCredentials() {
3735
String vcapServices = System.getenv("VCAP_SERVICES");
3836
logger.log(Level.INFO, "VCAP_SERVICES: \n" + vcapServices);
39-
try {
40-
if (vcapServices != null) {
41-
JsonNode mhub = parseVcapServices(vcapServices);
42-
ObjectMapper mapper = new ObjectMapper();
43-
return mapper.readValue(mhub.toString(), EventStreamsCredentials.class);
44-
} else {
45-
logger.log(Level.ERROR, "VCAP_SERVICES environment variable is null.");
46-
throw new IllegalStateException("VCAP_SERVICES environment variable is null.");
47-
}
48-
} catch (IOException ioe) {
49-
logger.log(Level.ERROR, "VCAP_SERVICES environment variable parses failed.");
50-
throw new IllegalStateException("VCAP_SERVICES environment variable parses failed.", ioe);
37+
if (vcapServices == null) {
38+
logger.log(Level.ERROR, "VCAP_SERVICES environment variable is null.");
39+
throw new IllegalStateException("VCAP_SERVICES environment variable is null.");
5140
}
41+
return transformVcapServices(vcapServices);
5242
}
5343

54-
private static JsonNode parseVcapServices(String vcapServices) throws IOException {
55-
ObjectMapper mapper = new ObjectMapper();
56-
JsonNode vcapServicesJson = mapper.readValue(vcapServices, JsonNode.class);
57-
58-
// when running in CloudFoundry VCAP_SERVICES is wrapped into a bigger JSON object
59-
// so it needs to be extracted. We attempt to read the "instance_id" field to identify
60-
// if it has been wrapped
61-
if (vcapServicesJson.get("instance_id") != null) {
62-
return vcapServicesJson;
63-
} else {
64-
String vcapKey = null;
65-
Iterator<String> it = vcapServicesJson.fieldNames();
66-
// Find the Event Streams service bound to this application.
67-
while (it.hasNext() && vcapKey == null) {
68-
String potentialKey = it.next();
69-
if (potentialKey.startsWith(SERVICE_NAME)) {
70-
logger.log(Level.WARN, "Using the '" + potentialKey + "' key from VCAP_SERVICES.");
71-
vcapKey = potentialKey;
44+
private static EventStreamsCredentials transformVcapServices(String vcapServices) {
45+
try {
46+
ObjectMapper mapper = new ObjectMapper();
47+
JsonNode instanceCredentials = mapper.readValue(vcapServices, JsonNode.class);
48+
// when running in CloudFoundry VCAP_SERVICES is wrapped into a bigger JSON object
49+
// so it needs to be extracted. We attempt to read the "instance_id" field to identify
50+
// if it has been wrapped
51+
if (instanceCredentials.get("instance_id") == null) {
52+
Iterator<String> it = instanceCredentials.fieldNames();
53+
// Find the Event Streams service bound to this application.
54+
while (it.hasNext()) {
55+
String potentialKey = it.next();
56+
String messageHubJsonKey = "messagehub";
57+
if (potentialKey.startsWith(messageHubJsonKey)) {
58+
logger.log(Level.WARN, "Using the '" + potentialKey + "' key from VCAP_SERVICES.");
59+
instanceCredentials = instanceCredentials.get(potentialKey)
60+
.get(0)
61+
.get("credentials");
62+
break;
63+
}
7264
}
7365
}
74-
75-
if (vcapKey == null) {
76-
throw new IllegalSaslStateException("No EventStreams service bound");
77-
} else {
78-
return vcapServicesJson.get(vcapKey).get(0).get("credentials");
79-
}
66+
return mapper.readValue(instanceCredentials.toString(), EventStreamsCredentials.class);
67+
} catch (IOException e) {
68+
logger.log(Level.ERROR, "VCAP_SERVICES environment variable parses failed.");
69+
throw new IllegalStateException("VCAP_SERVICES environment variable parses failed.", e);
8070
}
8171
}
8272
}

0 commit comments

Comments
 (0)