Add sample app for Pulsar Functions

See #135 #136
This commit is contained in:
Chris Bono
2023-01-09 19:10:17 -06:00
committed by Soby Chacko
parent 594361ef25
commit 195b2fb77b
21 changed files with 853 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
plugins {
id 'java'
}
group = 'org.springframework.pulsar.sample'
description = 'Sample Signup Pulsar Function'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.pulsar:pulsar-client-all:2.10.2'
implementation 'org.apache.pulsar:pulsar-functions-api:2.10.2'
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
}
// Customization of jar to make a simple Uber function jar
jar {
manifest {
attributes 'Main-Class': 'org.springframework.pulsar.sample.signup.SignupFunction'
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from(configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }) {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.pulsar.sample.signup;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
import org.springframework.pulsar.sample.signup.model.Customer;
import org.springframework.pulsar.sample.signup.model.Signup;
import org.springframework.pulsar.sample.signup.model.SignupTier;
public class SignupFunction implements Function<Signup, Void> {
@Override
public Void process(Signup signup, Context context) {
log("Processing " + signup);
// Count and log the signups for tier
SignupTier tier = signup.getSignupTier();
context.incrCounter(tier.name(), 1L);
long count = context.getCounter(tier.name());
log(String.format(" %s signup count: %d", tier.name(), count));
// Create customer onboard for enterprise signups
if (tier == SignupTier.ENTERPRISE) {
Customer customer = Customer.from(signup);
log("Converting to " + signup);
try {
context.newOutputMessage("customer-onboard", Schema.JSON(Customer.class))
.key(customer.getEmail())
.value(customer)
.eventTime(System.currentTimeMillis())
.send();
} catch (PulsarClientException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
return null;
}
private void log(String msg) {
// Typically logging is done via context.getLogger() but that logs to a topic
// which is not helpful during dev/debugging - sending to console for that reason
System.out.println(msg);
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.pulsar.sample.signup.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Customer {
private String firstName;
private String lastName;
private String email;
private long signupTimestamp;
public static Customer from(Signup signup) {
return new Customer(
signup.getFirstName(),
signup.getLastName(),
signup.getEmail(),
signup.getSignupTimestamp());
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.pulsar.sample.signup.model;
import lombok.Data;
@Data
public class Signup {
private SignupTier signupTier;
private String firstName;
private String lastName;
private String email;
private long signupTimestamp;
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.pulsar.sample.signup.model;
public enum SignupTier {
FREE,
BASIC,
STANDARD,
ENTERPRISE
}