简体   繁体   中英

How to call a method after a object from another class is created

First of all I want to show you my diagram: Diagram . How you can see, we have two modules "Ticket" and "Notification". The notification module depends on the ticket module. Once the "TicketService" created a ticket, the sendEmail method from the EmailHandler component shall be invoked. Unfortunately, I do not know how I can solve this.

Exists a pattern valid for any architecture. When your TicketService create a ticket, it generate an event and any interested process will subscribe.

In EJB it can be solved using @Observes annotation but any events broker (streaming... kafka, activemq, spark, ...) can be used too.

Spring Boot does not support directly event registration (like @Observes as described here ) but you can use it or any other event broker.

If you are not using Java enterprise services I recommend that you use a broker according to the needs of your project (eg kafka if it is big or nats for simplicity).

To show a minimal code example using NATS, you can connect to a NATS server broker using:

final Connection nc = Nats.connect("nats://localhost:4222");

then, any process interested in knowing that a new ticket has been created would listen for such events

final Subscription sub = c.subscribe(CHANNEL);
while (true)
    sendMessageForTicket(sub.nextMessage(Duration.ofDays(365)).getData());

the TicketService will send a message to be received by all these listening processes

c.publish(CHANNEL, myCreatedTicketData);

of course, all processes are decoupled, horizontally scalable (eg microservices) and only share the knowledge of the existence of a certain Ticket .

Your diagram explains the architecture (components and relations) while ignoring technical details (technologies/frameworks) In plain java. You can implement it quickly in plain java if you want.

  1. Create two java packages for 'ticket' and 'notification' modules.

  2. Create a java interface for EmailHandlerInterface and put it to the 'ticket' package.

  3. Inside the 'notification' package, Create EmailHandler class and implement created EmailHandlerInterface

  4. Inside the 'ticket' package, implement Ticket object as simple java POJO class,

  5. Inside the 'ticket' module, create the TicketSercice class that does take the Ticket object as a prototype and returns a created Ticket object.

  6. You need an additional object (let's name it TicketModuleExecutor or something) inside the "ticket package". You will have to inject an EmailHandler via EmailHandleInterface to it. This object should have a method 'execute' that creates a Ticket object, passes it to the TicketService, and executes after the successful TicketService::create call EmailHandler::sendEmail via interfaces.

  7. In the 'default' package, create the main application class with the main method (So you can execute your program). Inside the main, you initialize your 'ticket' module main class, initialize an EmailHandler object and pass it to the main course, and calls the TicketModuleExecutor.execute() to complete the whole program.

In general, if you follow all good practices like SOLID and IoC during implementation, you can later easily migrate your code to any framework without changing your architecture.

I might have made small mistakes; because it's so hard to code without coding using the English language, which is not your native language:-D But I will not do coding for you because I don't want to solve your task for you. You should implement the app yourself.

In Spring, you first should define what a module is for you. If it is a separate independent application, then choose communication method 1. Synchronous or 2. asynchronous. For the synchronous option, you can use rests or servlets. 2. For assychronous, you can use KAFKA, RabitMQ, JMS, or any other broker. If you want to have modules inside one application, you can use my plain java example and implement it using Spring Beans, or you can use spring events or the AOP approach. There are dozens of options for how this diagram could be implemented;-)

Anyway, for such a simple case, I recommend KISS (keep it simple stupid) implementing the application using Spring bean objects should suffice here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM