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) {
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) {
System.out.println("消费对象" + data);
}
}
使用xml引入外部文件
<bean class=”com.adtec.framework.common.util.ParamUtil”>
<property name=”locations”>
<array>
<value>file:${JAVAWORKDIR}/agent.properties</value>
<value>file:${JAVAWORKDIR}/cofig .properties</value>
</array>
</property>
</bean>
另一种xml方式
<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();
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;
}
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;
}
}