美文网首页Java 杂谈互联网科技程序员
涨知识丨JavaLib之消息引擎的妙用

涨知识丨JavaLib之消息引擎的妙用

作者: Java高级架构狮 | 来源:发表于2018-09-21 09:49 被阅读9次

    项目搭建

    1、你需要用Spring Boot搭建一个项目

    2、引入 JavaLib

    <dependencies>

    ...

    <dependency>

    <groupId>com.github.fengwenyi</groupId>

    <artifactId>JavaLib</artifactId>

    <version>1.0.4.RELEASE</version>

    </dependency>

    ...

    </dependencies>

    <repositories>

    <repository>

    <id>jitpack.io</id>

    <url>https://jitpack.io</url>

    </repository>

    </repositories>

    写代码

    1、编写Handler。需要实现 Handler 接口中的 execute方法。

    这里有两个,AliPayHandler, VXPayHandler。我们看一下 AliPayHandler

    import com.fengwenyi.javalib.handler.Handler;

    import com.fengwenyi.javalib.messageengine.CommonMessage;

    import com.fengwenyi.messageengine.MEConstant;

    import lombok.extern.slf4j.Slf4j;

    import org.springframework.stereotype.Component;

    /**

    * 支付宝支付Handler

    *

    * @author Wenyi Feng

    * @since 2018-09-06

    */

    @Component

    @Slf4j

    public class AliPayHandler implements Handler {

    @Override

    public void execute(CommonMessage message) throws Exception {

    log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

    log.info("支付方式:{}", "支付宝");

    log.info("姓名:{}", message.getHeader(MEConstant.NAME));

    log.info("账号:{}", MEConstant.ACCOUNT);

    log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

    }

    }

    2、编写Bean。

    import com.fengwenyi.javalib.handler.HandlerRegister;

    import com.fengwenyi.messageengine.handler.AliPayHandler;

    import com.fengwenyi.messageengine.handler.VXPayHandler;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    /**

    * Bean配置

    *

    * @author Wenyi Feng

    * @since 2018-09-06

    */

    @Configuration

    public class BeanConfig {

    @Autowired

    private AliPayHandler aliPayHandler;

    @Autowired

    private VXPayHandler vxPayHandler;

    @Bean

    public HandlerRegister messageEngineBean() {

    HandlerRegister handlerRegister = new HandlerRegister();

    handlerRegister.setHandler("ali", aliPayHandler);

    handlerRegister.setHandler("vx", vxPayHandler);

    return handlerRegister;

    }

    }

    3、写测试代码

    import com.fengwenyi.javalib.handler.Handler;

    import com.fengwenyi.javalib.handler.HandlerRegister;

    import com.fengwenyi.javalib.messageengine.CommonMessage;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication

    @RestController

    public class MessageEngineApplication {

    public static void main(String[] args) {

    SpringApplication.run(MessageEngineApplication.class, args);

    }

    @Autowired

    private HandlerRegister handlerRegister;

    @RequestMapping("/pay")

    public void pay(String type, String name, String account) {

    CommonMessage cm = new CommonMessage();

    cm.setHeader(MEConstant.NAME, name);

    cm.setHeader(MEConstant.ACCOUNT, account);

    Handler handler = handlerRegister.getHandler(type);

    try {

    handler.execute(cm);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    写在最后:欢迎留言讨论,加关注,持续更新!

    相关文章

      网友评论

        本文标题:涨知识丨JavaLib之消息引擎的妙用

        本文链接:https://www.haomeiwen.com/subject/dxqanftx.html