-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from lodsve/develop
2.6.7-RELEASE
- Loading branch information
Showing
40 changed files
with
557 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
lodsve-amqp/src/main/java/lodsve/amqp/annotations/AmqpBeanDefinitionRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
lodsve-amqp/src/main/java/lodsve/amqp/binding/DirectQueueBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
lodsve-amqp/src/main/java/lodsve/amqp/binding/FanoutQueueBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
lodsve-amqp/src/main/java/lodsve/amqp/configs/ExchangeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.