|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 3 | + * in compliance with the License. You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 8 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 9 | + * express or implied. See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. |
| 11 | + */ |
| 12 | +package io.streamnative.example; |
| 13 | + |
| 14 | +import io.streamnative.example.datagen.SignupGenerator; |
| 15 | +import io.streamnative.example.model.Customer; |
| 16 | +import io.streamnative.example.model.Signup; |
| 17 | +import io.streamnative.example.model.SignupTier; |
| 18 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 19 | +import org.apache.pulsar.client.impl.schema.JSONSchema; |
| 20 | +import org.apache.pulsar.common.schema.SchemaType; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.boot.SpringApplication; |
| 25 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 26 | +import org.springframework.pulsar.annotation.PulsarListener; |
| 27 | +import org.springframework.pulsar.core.PulsarTemplate; |
| 28 | +import org.springframework.scheduling.annotation.EnableScheduling; |
| 29 | +import org.springframework.scheduling.annotation.Scheduled; |
| 30 | + |
| 31 | +@EnableScheduling |
| 32 | +@SpringBootApplication |
| 33 | +public class SignupApplication { |
| 34 | + |
| 35 | + private static final Logger log = LoggerFactory.getLogger(SignupApplication.class); |
| 36 | + |
| 37 | + @Autowired private PulsarTemplate<Signup> signupTemplate; |
| 38 | + |
| 39 | + @Autowired private PulsarTemplate<Customer> customerTemplate; |
| 40 | + |
| 41 | + @Autowired private SignupGenerator signupGenerator; |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + SpringApplication.run(SignupApplication.class, args); |
| 45 | + } |
| 46 | + |
| 47 | + @Scheduled(initialDelay = 5000, fixedRate = 5000) |
| 48 | + void publishSignupData() throws PulsarClientException { |
| 49 | + Signup signup = signupGenerator.generate(); |
| 50 | + signupTemplate.setSchema(JSONSchema.of(Signup.class)); |
| 51 | + signupTemplate.send(signup); |
| 52 | + } |
| 53 | + |
| 54 | + @PulsarListener( |
| 55 | + subscriptionName = "signup-consumer", |
| 56 | + topics = "signups-topic", |
| 57 | + schemaType = SchemaType.JSON) |
| 58 | + void filterSignups(Signup signup) throws PulsarClientException { |
| 59 | + log.info( |
| 60 | + "{} {} ({}) just signed up for {} tier", |
| 61 | + signup.firstName(), |
| 62 | + signup.lastName(), |
| 63 | + signup.companyEmail(), |
| 64 | + signup.signupTier()); |
| 65 | + |
| 66 | + if (signup.signupTier() == SignupTier.ENTERPRISE) { |
| 67 | + Customer customer = Customer.from(signup); |
| 68 | + customerTemplate.setSchema(JSONSchema.of(Customer.class)); |
| 69 | + customerTemplate.send("customer-success", customer); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @PulsarListener( |
| 74 | + subscriptionName = "customer-consumer", |
| 75 | + topics = "customer-success", |
| 76 | + schemaType = SchemaType.JSON) |
| 77 | + void alertCustomerSuccess(Customer customer) { |
| 78 | + log.info( |
| 79 | + "## Start the onboarding for {} - {} {} ({}) - {} ##", |
| 80 | + customer.companyName(), |
| 81 | + customer.firstName(), |
| 82 | + customer.lastName(), |
| 83 | + customer.phoneNumber(), |
| 84 | + customer.companyEmail()); |
| 85 | + } |
| 86 | +} |
0 commit comments