java开源项目jeecg结构与代码全解析( 三 )


// 添加自己的过滤器并且取名为jwt
Map<string, filter=""> filterMap = new HashMap<string, filter="">(1);
//如果cloudServer为空 则说明是单体 需要加载跨域配置【微服务跨域切换】
Object cloudServer = env.getProperty(CommonConstant.CLOUD_SERVER_KEY);
//前文定义的过滤器
filterMap.put("jwt", new JwtFilter(cloudServer==null));
shiroFilterFactoryBean.setFilters(filterMap);
//
ex.printStackTrace();
}
}
}
}
public interface MqListener<t> {
default void handler(T map, Channel channel) {
}
}
</t></string,></string,></string,></string,>
2.RabbitMqClient
主要在队列初始化时实现队列的初始化 , 而是否初始化根据使用时的@RabbitListener、@RabbitComponent判断 。
public interface MqListener<t> {
default void handler(T map, Channel channel) {
}
}
@Bean
public void initQueue() {
//获取带RabbitComponent注解的类
Map<string, object=""> beansWithRqbbitComponentMap = this.applicationContext.getBeansWithAnnotation(RabbitComponent.class);
Class<!--? extends Object--> clazz = null;
//循环map
for (Map.Entry<string, object=""> entry : beansWithRqbbitComponentMap.entrySet()) {
log.info("初始化队列............");
//获取到实例对象的class信息
clazz = entry.getValue().getClass();
Method[] methods = clazz.getMethods();
//判断是否有RabbitListener注解
RabbitListener rabbitListener = clazz.getAnnotation(RabbitListener.class);
//类上有注解 就创建队列
if (ObjectUtil.isNotEmpty(rabbitListener)) {
createQueue(rabbitListener);
}
//方法上有注解 就创建队列
for (Method method : methods) {
RabbitListener methodRabbitListener = method.getAnnotation(RabbitListener.class);
if (ObjectUtil.isNotEmpty(methodRabbitListener)) {
createQueue(methodRabbitListener);
}
}
}
}
private void createQueue(RabbitListener rabbitListener) {
String[] queues = rabbitListener.queues();
//创建交换机
DirectExchange directExchange = createExchange(DelayExchangeBuilder.DELAY_EXCHANGE);
rabbitAdmin.declareExchange(directExchange);
//创建队列
if (ObjectUtil.isNotEmpty(queues)) {
for (String queueName : queues) {
Properties result = rabbitAdmin.getQueueProperties(queueName);
if (ObjectUtil.isEmpty(result)) {
Queue queue = new Queue(queueName);
addQueue(queue);
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(queueName);
rabbitAdmin.declareBinding(binding);
log.info("创建队列:" + queueName);
}else{
log.info("已有队列:" + queueName);
}
}
}
}
</string,></string,></t>
3.RabbitMqConfig
为消息队列的常用配置方式 。这里不多描述 。
4.event
这个包主要是为使用mq发送消息使用 , 多类别的消息会实现JeecgBusEventHandler类 , 而baseApplicationEvent通过消息类型传入的不同的参数选择合适的业务类发送消息 。
5.DelayExchangeBuilder
为延时队列的交换机声明与绑定 。
2.jeecg-boot-starter-lock
1.如何使用分布式锁
使用时有两种方式 , 一种是使用注解方式 , 一种是使用redisson提供的API 。
 
@Scheduled(cron = "0/5 * * * * ?")
@JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
public void execute() throws InterruptedException {
log.info("执行execute任务开始 , 休眠三秒");
Thread.sleep(3000);
System.out.println("=======================业务逻辑1=============================");
Map map = new baseMap();
map.put("orderId", "BJ0001");
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
//延迟10秒发送
map.put("orderId", "NJ0002");
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
log.info("execute任务结束 , 休眠三秒");
}
public DemoLockTest() {
}
//@Scheduled(cron = "0/5 * * * * ?")
public void execute2() throws InterruptedException {
if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, 6000)) {
log.info("执行任务execute2开始 , 休眠十秒");
Thread.sleep(10000);
System.out.println("=======================业务逻辑2=============================");
log.info("定时execute2结束 , 休眠十秒");


推荐阅读