简体   繁体   English

如何在创建另一个 class 的 object 后调用方法

[英]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".你怎么看,我们有两个模块“Ticket”和“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.一旦“TicketService”创建了一个工单,将调用 EmailHandler 组件的 sendEmail 方法。 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.当您的TicketService创建票证时,它会生成一个事件,任何感兴趣的进程都会订阅。

In EJB it can be solved using @Observes annotation but any events broker (streaming... kafka, activemq, spark, ...) can be used too.EJB中,它可以使用@Observes 注释来解决,但也可以使用任何事件代理(流媒体...kafka、activemq、spark...)。

Spring Boot does not support directly event registration (like @Observes as described here ) but you can use it or any other event broker. Spring Boot不支持直接事件注册(如此处所述@Observes ),但您可以使用它或任何其他事件代理。

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).如果您没有使用 Java 企业服务,我建议您根据项目的需要使用代理(例如kafka ,如果它很大,或者为了简单起见使用nats )。

To show a minimal code example using NATS, you can connect to a NATS server broker using:要显示使用 NATS 的最小代码示例,您可以使用以下方法连接到 NATS 服务器代理:

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 TicketService将发送一条消息以供所有这些侦听进程接收

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 .当然,所有进程都是解耦的、水平可扩展的(例如微服务)并且只共享某个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.您的图表解释了架构(组件和关系),同时忽略了技术细节(技术/框架)在普通 java 中。如果需要,您可以在普通 java 中快速实现它。

  1. Create two java packages for 'ticket' and 'notification' modules.为“票”和“通知”模块创建两个 java 包。

  2. Create a java interface for EmailHandlerInterface and put it to the 'ticket' package.为 EmailHandlerInterface 创建一个 java 接口,并将其放入 'ticket' package。

  3. Inside the 'notification' package, Create EmailHandler class and implement created EmailHandlerInterface在“通知”package 中,创建 EmailHandler class 并实现创建的 EmailHandlerInterface

  4. Inside the 'ticket' package, implement Ticket object as simple java POJO class,在“ticket”package 中,将 Ticket object 实现为简单的 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.在“票”模块中,创建以票 object 为原型的 TicketSercice class,并返回创建的票 object。

  6. You need an additional object (let's name it TicketModuleExecutor or something) inside the "ticket package".您需要在“票包”中添加一个额外的 object(我们将其命名为 TicketModuleExecutor 或其他名称)。 You will have to inject an EmailHandler via EmailHandleInterface to it.您必须通过 EmailHandleInterface 向其注入 EmailHandler。 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.这个 object 应该有一个方法 'execute' 来创建一个 Ticket object,将它传递给 TicketService,并在 TicketService::create 成功调用 EmailHandler::sendEmail 之后通过接口执行。

  7. In the 'default' package, create the main application class with the main method (So you can execute your program).在“默认”package 中,使用 main 方法创建主应用程序 class(这样您就可以执行您的程序)。 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.在主程序中,初始化“票”模块主程序 class,初始化一个 EmailHandler object 并将其传递给主程序,然后调用 TicketModuleExecutor.execute() 来完成整个程序。

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.一般来说,如果您在实施过程中遵循所有良好实践,如 SOLID 和 IoC,您以后可以轻松地将您的代码迁移到任何框架,而无需更改您的架构。

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.因为如果不使用英语编码就很难编码,这不是您的母语:-D 但我不会为您编码,因为我不想为您解决任务。 You should implement the app yourself.您应该自己实施该应用程序。

In Spring, you first should define what a module is for you.在 Spring 中,您首先应该定义适合您的模块。 If it is a separate independent application, then choose communication method 1. Synchronous or 2. asynchronous.如果是单独的独立应用,那么选择通信方式1.同步或者2.异步。 For the synchronous option, you can use rests or servlets. 2. For assychronous, you can use KAFKA, RabitMQ, JMS, or any other broker.对于同步选项,您可以使用 rests 或 servlets。 2. 对于异步,您可以使用 KAFKA、RabitMQ、JMS 或任何其他代理。 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.如果你想在一个应用程序中有模块,你可以使用我的普通 java 示例并使用 Spring Beans 实现它,或者你可以使用 spring 事件或 AOP 方法。 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.无论如何,对于这样一个简单的案例,我建议使用 Spring bean 对象实现应用程序的 KISS(保持简单愚蠢)在这里就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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