Skip to content

Commit

Permalink
Merge pull request #16 from lodsve/develop
Browse files Browse the repository at this point in the history
2.6.7-RELEASE
  • Loading branch information
sunhao-java authored Aug 28, 2018
2 parents 8576b67 + 80a6308 commit e227a9a
Show file tree
Hide file tree
Showing 40 changed files with 557 additions and 286 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 更新日志

## 2.6.7-RELEASE
1. 支持amqp注解,具体配置请参考`rabbit.properties`文件
2. 使用EnableXXX的配置文件都从注解中引入,其余通过spring.factories引入
3. 修改validate加载错误文件的一个bug

## 2.6.6-RELEASE
1. 整理配置文件,对必填项加上@Required
2. 增加事务开关
Expand Down
2 changes: 1 addition & 1 deletion lodsve-3rd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.6.6-RELEASE</version>
<version>2.6.7-RELEASE</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion lodsve-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>lodsve-framework</artifactId>
<groupId>com.lodsve</groupId>
<version>2.6.6-RELEASE</version>
<version>2.6.7-RELEASE</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion lodsve-amqp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.6.6-RELEASE</version>
<version>2.6.7-RELEASE</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.amqp.annotations;

import lodsve.amqp.binding.DirectQueueBinding;
import lodsve.amqp.binding.FanoutQueueBinding;
import lodsve.amqp.binding.TopicQueueBinding;
import lodsve.amqp.configs.ExchangeType;
import lodsve.amqp.configs.QueueConfig;
import lodsve.amqp.configs.RabbitProperties;
import lodsve.core.bean.BeanRegisterUtils;
import lodsve.core.properties.relaxedbind.RelaxedBindFactory;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.HashMap;
import java.util.Map;

/**
* 注册amqp的相关queue配置.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
* @date 2018-08-02 14:32
*/
public class AmqpBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
private RabbitProperties rabbitProperties;
private final Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>(16);

public AmqpBeanDefinitionRegistrar() {
rabbitProperties = new RelaxedBindFactory.Builder<>(RabbitProperties.class).build();
}

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Map<String, QueueConfig> queues = rabbitProperties.getQueues();

for (String queueName : queues.keySet()) {
QueueConfig config = queues.get(queueName);

// 1. 创建queue
createQueue(queueName, config.isDurable(), config.isExclusive(), config.isAutoDelete());
// 2. 创建binding
createBinding(queueName, config);
}

BeanRegisterUtils.registerBeans(beanDefinitionMap, registry);
}

private void createBinding(String queueName, QueueConfig config) {
ExchangeType type = config.getExchangeType();
Class<?> bindingRawClass;
Class<?> exchangeRawClass;
switch (type) {
case TOPIC:
bindingRawClass = TopicQueueBinding.class;
exchangeRawClass = TopicExchange.class;
break;
case DIRECT:
bindingRawClass = DirectQueueBinding.class;
exchangeRawClass = DirectExchange.class;
break;
case FANOUT:
bindingRawClass = FanoutQueueBinding.class;
exchangeRawClass = FanoutExchange.class;
break;
case HEADERS:
throw new RuntimeException("Lodsve-Framework not support HeadersExchange now!");
default:
bindingRawClass = null;
exchangeRawClass = null;
}

BeanDefinitionBuilder binding = BeanDefinitionBuilder.genericBeanDefinition(bindingRawClass);
binding.addConstructorArgReference(config.getExchangeName());
binding.addConstructorArgReference(queueName);
if (ExchangeType.FANOUT != type) {
binding.addConstructorArgValue(config.getRoutingKey());
}

beanDefinitionMap.put(config.getExchangeName() + "_queue_binding", binding.getBeanDefinition());

// 3. 创建exchange
if (!beanDefinitionMap.containsKey(config.getExchangeName())) {
BeanDefinitionBuilder exchange = BeanDefinitionBuilder.genericBeanDefinition(exchangeRawClass);
exchange.addConstructorArgValue(config.getExchangeName());
beanDefinitionMap.put(config.getExchangeName(), exchange.getBeanDefinition());
}

}

private void createQueue(String queueName, boolean durable, boolean exclusive, boolean autoDelete) {
BeanDefinitionBuilder queue = BeanDefinitionBuilder.genericBeanDefinition(Queue.class);
queue.addConstructorArgValue(queueName);
queue.addConstructorArgValue(durable);
queue.addConstructorArgValue(exclusive);
queue.addConstructorArgValue(autoDelete);

beanDefinitionMap.put(queueName, queue.getBeanDefinition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
@Inherited
@EnableLodsve
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitConfiguration.class)
@Import({AmqpBeanDefinitionRegistrar.class, RabbitConfiguration.class})
public @interface EnableAmqp {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.amqp.binding;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;

import java.util.Collections;

/**
* direct queue.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
* @date 2018-08-02 13:49
*/
public class DirectQueueBinding extends Binding {
public DirectQueueBinding(DirectExchange exchange, Queue queue, String routingKey) {
super(queue.getName(), DestinationType.QUEUE, exchange.getName(), routingKey, Collections.<String, Object>emptyMap());
}

public DirectQueueBinding(String exchange, String queue, String routingKey) {
super(queue, DestinationType.QUEUE, exchange, routingKey, Collections.<String, Object>emptyMap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.amqp.binding;

import lodsve.core.utils.StringUtils;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;

import java.util.Collections;

/**
* fanout queue.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
* @date 2018-08-02 13:48
*/
public class FanoutQueueBinding extends Binding {
public FanoutQueueBinding(FanoutExchange exchange, Queue queue) {
super(queue.getName(), DestinationType.QUEUE, exchange.getName(), StringUtils.EMPTY, Collections.<String, Object>emptyMap());
}

public FanoutQueueBinding(String exchange, String queue) {
super(queue, DestinationType.QUEUE, exchange, StringUtils.EMPTY, Collections.<String, Object>emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.amqp.core;
package lodsve.amqp.binding;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;

import java.util.Collections;

/**
* .
* topic queue.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
* @date 2017/12/7 11:18
* @date 2018-08-02 13:49
*/
public class QueueBinding extends Binding {
public QueueBinding(FanoutExchange exchange, Queue queue) {
super(queue.getName(), DestinationType.QUEUE, exchange.getName(), null, Collections.<String, Object>emptyMap());
}
public class TopicQueueBinding extends Binding {

public QueueBinding(Exchange exchange, Queue queue, String routingKey) {
public TopicQueueBinding(TopicExchange exchange, Queue queue, String routingKey) {
super(queue.getName(), DestinationType.QUEUE, exchange.getName(), routingKey, Collections.<String, Object>emptyMap());
}

public QueueBinding(String exchange, String queue, String routingKey) {
public TopicQueueBinding(String exchange, String queue, String routingKey) {
super(queue, DestinationType.QUEUE, exchange, routingKey, Collections.<String, Object>emptyMap());
}
}
43 changes: 43 additions & 0 deletions lodsve-amqp/src/main/java/lodsve/amqp/configs/ExchangeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.amqp.configs;

/**
* amqp exchange type.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
* @date 2018-08-02 14:53
*/
public enum ExchangeType {
/**
* direct
*/
DIRECT,
/**
* topic
*/
TOPIC,
/**
* fanout
*/
FANOUT,
/**
* header
*/
HEADERS
}
Loading

0 comments on commit e227a9a

Please sign in to comment.