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

Commit

Permalink
springboot transactional message
Browse files Browse the repository at this point in the history
  • Loading branch information
zh_yu committed Jan 29, 2019
1 parent d58cd21 commit 3b5f52f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 3 deletions.
8 changes: 8 additions & 0 deletions qmq-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.qunar.qmq</groupId>
<artifactId>qmq</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package qunar.tc.qmq.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
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.consumer.annotation.EnableQmq;
import qunar.tc.qmq.producer.MessageProducerProvider;
import qunar.tc.qmq.producer.tx.spring.SpringTransactionProvider;

import javax.sql.DataSource;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -32,10 +35,14 @@
public class BeanConfig {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
return dataSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = "qunar.tc.qmq.demo.dao")
@EntityScan(basePackages = "qunar.tc.qmq.demo.model")
public class JpaConfiguration {

}
25 changes: 25 additions & 0 deletions qmq-demo/src/main/java/qunar/tc/qmq/demo/dao/OrderRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import qunar.tc.qmq.demo.model.Order;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}
11 changes: 10 additions & 1 deletion qmq-demo/src/main/java/qunar/tc/qmq/demo/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@

package qunar.tc.qmq.demo.model;

public class Order {
import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "orders")
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private long orderId;

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import qunar.tc.qmq.Message;
import qunar.tc.qmq.MessageProducer;
import qunar.tc.qmq.MessageSendStateListener;
import qunar.tc.qmq.demo.dao.OrderRepository;
import qunar.tc.qmq.demo.model.Order;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class OrderService {
Expand All @@ -33,7 +35,12 @@ public class OrderService {
@Resource
private MessageProducer producer;

@Resource
private OrderRepository orderRepository;

@Transactional
public void placeOrder(Order order) {
orderRepository.save(order);
final Message message = producer.generateMessage("order.changed");
message.setProperty("orderId", order.getOrderId());
message.setProperty("name", order.getName());
Expand Down
12 changes: 11 additions & 1 deletion qmq-demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ metaServer=http://127.0.0.1:8080/meta/address

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

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/order_db
spring.datasource.username=abc
spring.datasource.password=123456
spring.jpa.database=MYSQL
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

0 comments on commit 3b5f52f

Please sign in to comment.