springboot 使用rockmq并引入外部文件

pom文件引入rocketmq的starter

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

创建生产者

@Service
public class ProducerZkMessageImpl {

	@Resource
	private RocketMQTemplate rocketMQTemplate;

	public void sendMessage(Object data) {
		if (data != null) {
			// 发送同步顺序消息,发送到broker,broker服务器会返回结果sendResult
			SendResult synSend = rocketMQTemplate.syncSendOrderly("NodeDataMSGA:normal", data, "orderId");
			log.debug("{}", synSend);
		} else {
			log.debug("发送的消息为空");
		}
	}

	public void sendMessageString(String data) {
		SendResult syncSend = rocketMQTemplate.syncSendOrderly("NodeDataMSGA:string", data, "orderId");
		log.debug("{}", syncSend);
	}
}

创建消费者

@Service
@RocketMQMessageListener(topic = "NodeDataMSGA", consumerGroup = "NodeData-groupTAG", 
	selectorExpression = "string")
public class CousmerMessageStringController implements RocketMQListener<String> {
	
	@Override
	public void onMessage(String zipName) {
		System.out.println("消费string" + zipName);
	}
}
@Service
@RocketMQMessageListener(topic = "${CousmerMessageA.topic}", 
	consumerGroup = "${CousmerMessageA.consumerGroup}", selectorExpression = "normal")
public class CousmerMessageController implements RocketMQListener<Object> {

	@Override
	public void onMessage(Object data) {
		// 监听到zk节点做的操作类型(增加,修改,删除)
		System.out.println("消费对象" + data);
	}
}

使用xml引入外部文件

<bean class=”com.adtec.framework.common.util.ParamUtil”>
	<property name=”locations”><!--PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->
		<array>
			<!--      <value>classpath:agent.properties</value>-->
			<!--      <value>classpath:config .properties</value>-->
			<value>file:${JAVAWORKDIR}/agent.properties</value>
			<value>file:${JAVAWORKDIR}/cofig .properties</value>
		</array>
	</property>
</bean>

另一种xml方式

<!--加载应用属性实例,可通过  @Value(“#{APP_PROP[`jdbc.driver`]}”)String jdbcDriver 方式引用-->
<util:properties id=”APP-PROP” location =:file:${JAVAWORKDIR}/config.properties”local-override=”true”/>

使用代码进行引入外部文件方式

@Component
public class Common {
	@Autowired
	private ConfigurableEnvironment environment;

	@PostConstruct
	public void setProperties(){
		MutablePropertySources propertySources=environment.getPropertySources();
		for(PropertySource<?> propertySource:propertySources){
			if(propertySource.getName().indexOf("application.yml")!=-1){
				Properties properties=new Properties();
				//对应 CousmerMessageController 中的注解
				properties.put("CousmerMessageA.topic","NodeDataMGA");
				properties.put("CousmerMessageA.consumerGroup","NodeData-group");
				PropertiesPropertySource constants=new PropertiesPropertySource("system.config",properties);
				propertySources.addAfter(propertySource.getName(),constants);
			}
		}
	}

	public Properties getZKCacheProperties(String env){
		String envPath="/home/app/zk-cache.properties";
		Properties properties=null;
		try{
			properties=PropertiesLoaderUtils.loadAllProperties(envPath);
		}catch(IOException e){
			e.printStackTrace();
		}
		return properties;
	}

	/**
	 * 读取外部配置文件
	 * @return
	 */
	public Properties getfileData(){
		//读取配置文件
		Properties properties=new Properties();
		//获取配置文件路径
		String filePath="/home/app/fileName.properties";
		File file=new File(filePath);
		FileInputStream fis=null;
		try{
			fis=new FileInputStream(file);
			properties.load(new InputStreamReader(fis,"UTF-8"));
		}catch(FileNotFoundException e){
			throw new RuntimeException(e);
		}catch(IOException e) {
			throw new RuntimeException(e);
		}finally {
			try {
				if(fis!=null) {
					fis.close();
				}
			}catch(IOException e){
				
			}
		}
		//获取配置文件数据
		return properties;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值