Disruptor处理(IO密集型)并发1000*1000的数据例子

与上一个例子的Disruptor处理(计算密集型)并发1000*6000的数据例子 的order类,producer类 consumer类一样,需要读取数据库,需要增加的内容有:

1、pom.xml中增加

 <!-- 对jsp的支持 -->
    <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!-- 对servlet的支持 -->
    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
    </dependency>
     <!-- 对jstl的支持 -->
    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
    </dependency>
     <!-- 引入jpa的依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
   <!-- 引入对druid的最新依赖 -->
    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.0.29</version>
    </dependency>
    <!-- 引入mysql的依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- fastjson的依赖 -->
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.58</version>
  </dependency>
   <!-- 增加mybatis支持 -->
  <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.2.0</version>
  </dependency>
  <!-- 增加事务的支持 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

2、增加application.yml文件

server:
  port: 9009
spring: 
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wj?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: root123
  mvc:
    static-path-pattern: /**
  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/
##filters:stat,wall,log4j
mybatis:
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.disruptor.demo.pojo
  configuration:
    map-underscore-to-camel-case: true

3、增加pojo  dao service serviceimpl文件

@Entity
@Table(name= "userinfo")
public class UserInfo implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8860628508832355043L;

	@Id
	@GeneratedValue
	@Column(name= "id")
	private Long id;
	
	private String username;
	private String password;
    private Date insertDate;
    private String realName;
    private Integer bonus;
    private String position;
    private String keywords;
public interface UserDAO{
	public UserInfo findByUserName(UserInfo staffInfo) throws Exception; 
	/**
	   * 查询用户列表
	 * @param staffInfo
	 * @return
	 * @throws Exception
	 */
	public List<UserInfo> getUserList(UserInfo staffInfo) throws Exception;
	
	/**
	 * 
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public UserInfo getUserById(UserInfo userInfo) throws Exception;
	/**
	 * 
	 * @param userInfo
	 * @throws Exception
	 */
	public void deleteUserById(UserInfo userInfo) throws Exception;
	
	public void addUserInfo(UserInfo userInfo) throws Exception;
	
	public void updateUsersById(UserInfo userInfo) throws Exception;
	
}
public interface UserService {
	public UserInfo findByUserName(UserInfo staffInfo) throws Exception; 
	/**
	   * 查询用户列表
	 * @param staffInfo
	 * @return
	 * @throws Exception
	 */
	public List<UserInfo> getUserList(UserInfo staffInfo) throws Exception;
	
	/**
	 * 
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public UserInfo getUserById(UserInfo userInfo) throws Exception;
	/**
	 * 
	 * @param userInfo
	 * @throws Exception
	 */
	public void deleteUserById(UserInfo userInfo) throws Exception;
	
	public void addUserInfo(UserInfo userInfo) throws Exception;
	
	public void updateUsersById(UserInfo userInfo) throws Exception;
}
@Service("UserService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;
	
	public UserInfo findByUserName(UserInfo staffInfo) throws Exception{
		 return userDAO.findByUserName(staffInfo);
	}
	/**
	   * 查询用户列表
	 * @param staffInfo
	 * @return
	 * @throws Exception
	 */
	public List<UserInfo> getUserList(UserInfo staffInfo) throws Exception{
		return userDAO.getUserList(staffInfo);
	}
	
	/**
	 * 
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public UserInfo getUserById(UserInfo userInfo) throws Exception{
		return userDAO.getUserById(userInfo);
	}
	/**
	 * 
	 * @param userInfo
	 * @throws Exception
	 */
	public void deleteUserById(UserInfo userInfo) throws Exception{
		userDAO.deleteUserById(userInfo);
	}
	
	public void addUserInfo(UserInfo userInfo) throws Exception{
		userDAO.addUserInfo(userInfo);
	}
	
	public void updateUsersById(UserInfo userInfo) throws Exception{
		userDAO.updateUsersById(userInfo);
	}

}

4、新建一个mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="com.disruptor.demo.dao.UserDAO">


  <select id="getUserList" parameterType="UserInfo" resultType="UserInfo">
    SELECT
    *
  FROM
    userinfo A 
    where 1=1
    <if test="keywords != null and '' != keywords">  
        <bind name="pattern_keywords" value="'%'+keywords+'%'"/>
            <![CDATA[  
              AND (A.username like #{pattern_keywords}  OR A.realName like #{pattern_keywords} OR A.position like #{pattern_keywords})
            ]]>  
     </if>  
  </select>
   <select id="getUserById" parameterType="UserInfo" resultType="UserInfo">
    SELECT
    *
  FROM
    userinfo
    WHERE id = #{id}
  </select>
  
  
  
  
</mapper>

5、在controller中间数据中查到的用户中的bonus作为参数进行数据计算。

@RestController
public class MoreDisruptorController {
	
	@Autowired
    UserService userService;
	
	
	@GetMapping("/api/test4")
    public void demo4() throws InterruptedException{
		// 创建ringBuffer
		RingBuffer<Order> ringBuffer = RingBuffer.create(ProducerType.MULTI, new EventFactory<Order>() {
			@Override
			public Order newInstance() {
				return new Order();
			}
		}, 1024 * 1024, new YieldingWaitStrategy());

		SequenceBarrier barrier = ringBuffer.newBarrier();

		Consumer[] consumers = new Consumer[2];
		for (int i = 0; i < consumers.length; i++) {
			consumers[i] = new Consumer("c" + i);
		}

		WorkerPool<Order> workerPool = new WorkerPool<Order>(ringBuffer, barrier, new IntEventExceptionHandler(),consumers);
		ringBuffer.addGatingSequences(workerPool.getWorkerSequences());
		workerPool.start(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));

		final CountDownLatch latch = new CountDownLatch(1);
		for (int i = 0; i < 1000; i++) {
			final ProducerIO p = new ProducerIO(ringBuffer);
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						latch.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					for (int j = 0; j < 1000; j++) {						
						long busId = j+2;
						 UserInfo requestUser = new UserInfo();
						 long id = busId;
						 requestUser.setId(id);
						 UserInfo user;
						try {
							user = userService.getUserById(requestUser);
							double price = 1 + user.getBonus();
							p.onData(UUID.randomUUID().toString(),price);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}			
							
					}
				}
			}).start();
		}
		Thread.sleep(2000);
		System.out.println("------------开始-------------");
		
		latch.countDown();
		
		Thread.sleep(100000);
		
		System.out.println("总数:" + consumers[0].getCount());	
	}
	static class IntEventExceptionHandler implements ExceptionHandler {
		@Override
		public void handleEventException(Throwable arg0, long arg1, Object arg2) {
		}

		@Override
		public void handleOnShutdownException(Throwable arg0) {
		}

		@Override
		public void handleOnStartException(Throwable arg0) {
		}
	}

}

6、执行结果:

在地址栏中输入:

需要读取数据库,运行比较慢,3分钟左右运行完1000000数据。没有读取数据库直接按照随机数计算比较快,详见上篇。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值