通过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());
}
}
验证数据正确,同时看到测试控制台有数据输出.