Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
add qmq-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zh_yu committed Jan 23, 2019
1 parent 7b3281e commit b6395f6
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<module>qmq-dist</module>
<module>qmq-watchdog</module>
<module>qmq-gateway</module>
<module>qmq-demo</module>
</modules>

<properties>
Expand Down
45 changes: 45 additions & 0 deletions qmq-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>qmq-parent</artifactId>
<groupId>com.qunar.qmq</groupId>
<version>1.1.3.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>qmq-demo</artifactId>

<properties>
<spring-boot.version>1.5.10.RELEASE</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.qunar.qmq</groupId>
<artifactId>qmq</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.qunar.qmq</groupId>
<artifactId>qmq</artifactId>
</dependency>
</dependencies>
</project>
28 changes: 28 additions & 0 deletions qmq-demo/src/main/java/qunar/tc/qmq/demo/SpringBootMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootMain extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
50 changes: 50 additions & 0 deletions qmq-demo/src/main/java/qunar/tc/qmq/demo/config/BeanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import qunar.tc.qmq.MessageProducer;
import qunar.tc.qmq.producer.MessageProducerProvider;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class BeanConfig {

@Bean
public MessageProducer producer(@Value("${appCode}") String appCode, @Value("${metaServer}") String metaServer) {
final MessageProducerProvider producer = new MessageProducerProvider();
producer.setAppCode(appCode);
producer.setMetaServer(metaServer);
return producer;
}

/*
处理消息的业务线程池,线程池队列不要设置太大
*/
@Bean(name = "workerExecutor")
public ThreadPoolExecutor workerExecutor(@Value("${executor.coreSize}") int core,
@Value("${executor.maxSize}") int max,
@Value("${executor.queueSize}") int queueSize) {
return new ThreadPoolExecutor(core, max, 1,
TimeUnit.MINUTES, new LinkedBlockingQueue<>(queueSize));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qunar.tc.qmq.demo.model.Order;
import qunar.tc.qmq.demo.service.OrderService;

import javax.annotation.Resource;

@RequestMapping("order")
@RestController
public class OrderController {
@Resource
private OrderService orderService;

@PostMapping("/placeOrder")
public void placeOrder(long orderId, String name) {
Order order = new Order();
order.setOrderId(orderId);
order.setName(name);
orderService.placeOrder(order);
}

}
39 changes: 39 additions & 0 deletions qmq-demo/src/main/java/qunar/tc/qmq/demo/model/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo.model;

public class Order {
private long orderId;

private String name;

public long getOrderId() {
return orderId;
}

public void setOrderId(long orderId) {
this.orderId = orderId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo.service;

import org.springframework.stereotype.Service;
import qunar.tc.qmq.Message;
import qunar.tc.qmq.consumer.annotation.QmqConsumer;

@Service
public class OrderChangedConsumer {

@QmqConsumer(subject = "order.changed", consumerGroup = "ordercenter", executor = "workerExecutor")
public void onMessage(Message message) {
long orderId = message.getLongProperty("orderId");
String name = message.getStringProperty("name");

//do work
}
}
52 changes: 52 additions & 0 deletions qmq-demo/src/main/java/qunar/tc/qmq/demo/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018 Qunar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package qunar.tc.qmq.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import qunar.tc.qmq.Message;
import qunar.tc.qmq.MessageProducer;
import qunar.tc.qmq.MessageSendStateListener;
import qunar.tc.qmq.demo.model.Order;

import javax.annotation.Resource;

@Service
public class OrderService {
private static final Logger LOG = LoggerFactory.getLogger(OrderService.class);

@Resource
private MessageProducer producer;

public void placeOrder(Order order) {
final Message message = producer.generateMessage("order.changed");
message.setProperty("orderId", order.getOrderId());
message.setProperty("name", order.getName());
producer.sendMessage(message, new MessageSendStateListener() {
@Override
public void onSuccess(Message message) {
LOG.info("send message success: {}", message.getMessageId());
}

@Override
public void onFailed(Message message) {
LOG.error("send message failed: {}", message.getMessageId());
}
});
}
}
6 changes: 6 additions & 0 deletions qmq-demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
appCode=qmq_test
metaServer=http://127.0.0.1:8080/meta/address

executor.coreSize=2
executor.maxSize=2
executor.queueSize=100

0 comments on commit b6395f6

Please sign in to comment.