Spring Boot JMS

通过Spring Boot能够更方便的引入ActiveMQ和JMS基础设置.
在pom.xml中加入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

查看依赖:

mvn dependency:tree

jms和activemq已经引入:

[INFO] +- org.springframework.boot:spring-boot-starter-activemq:jar:1.4.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-jms:jar:4.3.7.RELEASE:compile
[INFO] |  \- org.apache.activemq:activemq-broker:jar:5.13.5:compile

在Application.java中引入注解加入配置:

@SpringBootApplication
@EnableScheduling
@EnableJms
public class Application {    
    public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();        
        configurer.configure(factory, connectionFactory);        
        return factory;
    }

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }
}

创建Domain, DataItem,使用了lombok来简化类创建,引入jsonProperty直接映射为JSON

@Data
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataItem {    
    @JsonProperty(value = "$time")
    private long timestamp;
    private String data;
} 

创建Receiver,指定destination和containerFactory,

@Component
public class DataReceiver {
    private final static Logger LOGGER = LoggerFactory.getLogger(DataReceiver.class);

    @JmsListener(destination = "dataReceiver", containerFactory = "myFactory")
    public void receiveMessage(DataItem item) {
        LOGGER.info("Received: {}", item);
    }
}

创建Controller,当收到推送数据时发送后台处理消息,

@RestController
@RequestMapping("/apiv1")
public class MyRestController {
    @Autowired
    private JmsTemplate jmsTemplate;

    @RequestMapping(value = "/data", method = RequestMethod.POST) 
    public String receiveData(HttpServletRequest request, @RequestBody String dataStr) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        item = mapper.readValue(dataStr, DataItem.class);
        jmsTemplate.convertAndSend("dataReceiver", item);
    }
}

最后编写测试用例,

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class LoraRestControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void loraDataTest() throws Exception {
        String uri = "/apiv1/data";
        String json ="{\"$time\":1499745787080,\"data\":\"55ff1010012162\"}";

        mockMvc.perform(
        post(uri, "json").characterEncoding("UTF-8").contentType(MediaType.APPLICATION_JSON).content(json)
                )
                .andExpect(status().isOk());
    }
}

验证数据正确,同时看到测试控制台有数据输出.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值