Java實現高並發秒殺API--Service層2

來源:互聯網
上載者:User

標籤:業務   sed   unit   else   one   bean   erro   message   請求   

今天完成了整個Java實現高並發秒殺API--Service層的學習:

1、介面的編碼以及實作類別的邏輯編寫

2、利用spring ioc對Service進行管理

3、利用spring聲明式事務對事務進行控制:

事務主要配置:

<!--配置交易管理員 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置基於註解的聲明式事務 預設使用註解來管理事務行為 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

並且瞭解到spring使用事務的3種方式,最終決定使用聲明式事務,原因如下:

     * 1、Team Dev達成一致約定,明確標註事務方法的變成風格
     * 2、保證事務方法的執行時間儘可能短,不要穿插其他網路操作,RPC/HTTP請求或者剝離到事務方法外
     * 3、不是所有的方法都需要事務,如只有一條修改操作,唯讀操作不需要事務控制

最後使用了junit進行整合測試,測試的注意點為

在對介面方法進行測試完成之後還需要對商務邏輯進行測試。即可能存在2個介面通過一定商務程序的綁定測試。

    @Test
    public void testSeckillLogic() {
        long id = 1001L;
        Exposer exportSeckillUrl = seckillService.exportSeckillUrl(id);
        if (exportSeckillUrl.isExposed()) {
            logger.info("exposer={}", exportSeckillUrl);
            long phone = 15068751236L;
            String md5 = exportSeckillUrl.getMd5();
            try {
                SeckillExectuion executeSeckill = seckillService.executeSeckill(id, phone, md5);
                logger.info("result={}", executeSeckill);
            } catch (RepeatKillException e) {
                logger.error(e.getMessage());
            } catch (SeckillCloseException e) {
                logger.error(e.getMessage());
            }
        } else {
            logger.warn("exposer={}", exportSeckillUrl);
        }

 

Java實現高並發秒殺API--Service層2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.