spring boot 消费activemq ,推送到kafka例子

一:加入依赖

    
            org.springframework.boot
            spring-boot-starter-activemq
        

        
            org.springframework.kafka
            spring-kafka
            

 

二:application.properties 配置

 

spring.activemq.broker-url=tcp://10.1.1.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=true
spring.activemq.packages.trust-all=true


#kafka
spring.kafka.bootstrap-servers=10.11.41.1:9092
#spring.kafka.consumer.bootstrap-servers=10.11.1.108:9092
#spring.kafka.producer.bootstrap-servers=10.1.1.1:9092
spring.kafka.consumer.group-id=foo
#spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.properties.sasl.mechanism=PLAIN
spring.kafka.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="alice" password="alice-secret";

 

三:实现代码

@Component
public class ActiveMqConsumer {
    public static Logger logger = LoggerFactory.getLogger(ActiveMqConsumer.class);
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private KafkaTemplate template;

    @JmsListener(destination = "TOPIC", containerFactory = "myFactory")
    public void receiveMsg(Message text)
            throws Exception {
        MapMessage message = (MapMessage) text;
        this.template.send("FACEIMAGE.CHANCHENG.TEST", message.toString());
    }

    @Bean
    public JmsListenerContainerFactory myFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(true);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}
 

 

你可能感兴趣的:(java技术)