前言
假设我要配置一个TiEasyRocketProducer bean,但是测试环境下可能不一定有它的依赖类DefaultMQProducer.class,这个时候就可以根据@ConditionalOnClass和@Profile来动态创建bean。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import com.alibaba.rocketmq.client.producer.DefaultMQProducer; import com.alibaba.rocketmq.client.producer.SendStatus; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import org.apache.commons.lang.BooleanUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.UUID; public class TiEasyRocketProducer { private static final Logger LOGGER = LoggerFactory.getLogger(MiProducer.class); private Cache<String, Producer> producerCache = CacheBuilder.newBuilder().build(); private DefaultMQProducer producer; public MiProducer(String groupName) throws Exception { producer = new DefaultMQProducer(groupName); producer.setNamesrvAddr(namesrvUrl); producer.setSendMsgTimeout(12000); producer.setCompressMsgBodyOverHowmuch(4096); producer.setInstanceName(UUID.randomUUID().toString().replace("-", "")); producer.start(); } public Boolean sendMessage(String topic, String tags, String content, int delayLevel) throws Exception{ return this.sendMessage(topic, tags, content, delayLevel, null); } public Boolean sendMessage(String topic, String tags, String content) throws Exception{ return this.sendMessage(topic, tags, content, 0, null); } public Boolean sendMessage(String topic, String tags, String content, String businessId) throws Exception{ return this.sendMessage(topic, tags, content, 0, businessId); } public Boolean sendMessage(String topic, String tags, String content, int delayLevel, String businessId) throws Exception { byte[] body = content.getBytes(StandardCharsets.UTF_8); com.alibaba.rocketmq.common.message.Message message = new com.alibaba.rocketmq.common.message.Message(topic, tags, body); if (delayLevel > 0) message.setDelayTimeLevel(delayLevel); com.alibaba.rocketmq.client.producer.SendResult sendResult = producer.send(message); LOGGER.debug("message send result: {}", sendResult); return sendResult.getSendStatus() == SendStatus.SEND_OK || sendResult.getSendStatus() == SendStatus.SLAVE_NOT_AVAILABLE; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.alibaba.rocketmq.client.producer.DefaultMQProducer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @ConditionalOnClass(DefaultMQProducer.class) public class RocketMQConfig { @Value("${spring.application.name}") private String appName; @Bean("testProducer") @Profile("test") public TiEasyRocketProducer testProducer() throws Exception { return new TiEasyRocketProducer(appName); } } |
0