【程序媛晒83行代码】认真工作的程序媛原来是这样,你想到了嘛?

在中国程序媛中,他们的代码有什么样的魅力,Aone联合云栖社区、饿了么、钉钉、阿里云、天猫、口碑发起首届程序媛比码活动——不秀大长腿,秀高智商;不秀美图照,秀代码图,参与晒码互动游戏赢“83行代码”T恤!

我们来说说这群女工程师的第83行代码及代码背后的故事:
我是云效的全栈开发女工程师,朱默,花名:墨瑜。

据说认真工作的程序员魅力值加10:
【程序媛晒83行代码】认真工作的程序媛原来是这样,你想到了嘛?
我的第83行代码来自云效RDC上某一功能的代码, 猜猜是哪个功能:

public class MysqlLockBuilder {
    private DataSource dataSource;
    private String mysqlTableName;

    private LockDataAccessor lockDataAccessor;

    private LockStateAccessor lockStateAccessor;
    private String defaultClientId;

    public void init() throws SQLException {
        MysqlLockDataAccessor mysqlLockDataAccessor = new MysqlLockDataAccessor();
        mysqlLockDataAccessor.setMysqlTableName(mysqlTableName);
        mysqlLockDataAccessor.setConnection(dataSource.getConnection());
        lockDataAccessor = mysqlLockDataAccessor;

        defaultClientId = IP.getIpAddr();

        lockStateAccessor = buildMysqlLockStateAccessor();
    }

    private LockStateAccessor buildMysqlLockStateAccessor() throws SQLException {
        MysqlLockStateAccessor lockStateAccessor = new MysqlLockStateAccessor();
        lockStateAccessor.setMysqlTableName(mysqlTableName);
        lockStateAccessor.setConnection(dataSource.getConnection());
        lockStateAccessor.init();
        return lockStateAccessor;
    }

    public String getDefaultClientId() {
        return defaultClientId;
    }

    public void setDefaultClientId(String defaultClientId) {
        this.defaultClientId = defaultClientId;
    }

    public TimedLock lockForAWhile(String lockId, int timeout) throws LockAccessException {
        TimedLock lock = new TimedLock(lockId, defaultClientId, timeout);
        try {
            lock.setLockStateAccessor(buildMysqlLockStateAccessor());
            lock(lock);
            return lock;
        } catch (SQLException e){
            lock.close();
            throw new LockAccessException(e);
        } catch (LockAccessException e) {
            lock.close();
            throw e;
        }
    }

    public ScopedLock lockWhenAlive(String lockId) throws LockAccessException {
        ScopedLock lock = new ScopedLock(lockId, defaultClientId);
        try {
            lock.setLockStateAccessor(buildMysqlLockStateAccessor());
            lock(lock);
            return lock;
        } catch (SQLException e){
            lock.close();
            throw new LockAccessException(e);
        } catch (LockAccessException e){
            lock.close();
            throw e;
        }
    }

    /**
     * 指定clientId,加时间锁
     * @param lockId: 锁名称
     * @param timeout: 加锁时长
     * @param clientId: 锁的持有者
     * @return
     * @throws LockAccessException
     */
    public TimedLock lockForAWhileByClient(String lockId, int timeout, String clientId) throws LockAccessException {
        TimedLock lock = new TimedLock(lockId, clientId, timeout);
        try {
            LockStateAccessor timeLockStateAccessor = buildMysqlLockStateAccessor();
            lock.setLockStateAccessor(timeLockStateAccessor);
            lock(lock);
            return lock;
        } catch (SQLException e){
            lock.close();
            throw new LockAccessException(e);
        } catch (LockAccessException e) {
            lock.close();
            throw e;
        }
    }

    /**
     * 刷新时间锁,延长加锁时长
     * @param lockingContext
     * @return
     * @throws LockAccessException
     */
    public Boolean refreshTimeLockByContext(LockingContext lockingContext) throws LockAccessException {
        if(lockStateAccessor == null){
            try {
                lockStateAccessor = buildMysqlLockStateAccessor();
            }catch (SQLException e) {
                throw new LockAccessException(e);
            }
        }

        return lockStateAccessor.refreshTimeLock(lockingContext);
    }

    /**
     * 手动解锁
     * @param lockingContext
     * @return
     * @throws LockAccessException
     */
    public Boolean unlockTimeLockByContext(LockingContext lockingContext) throws LockAccessException {
        if(lockStateAccessor == null){
            try {
                lockStateAccessor = buildMysqlLockStateAccessor();
            }catch (SQLException e) {
                throw new LockAccessException(e);
            }
        }

        return lockStateAccessor.timedUnlock(lockingContext);
    }

    /**
     * 根据锁id,获取时间锁信息
     * @param lockId
     * @return
     * @throws LockAccessException
     */
    public LockingInfo getTimedLockInfo(String lockId) throws LockAccessException{
        return lockStateAccessor.getTimedLockingInfo(lockId);
    }

    private void lock(Lockable lock) throws LockAccessException {
        try {
            lockDataAccessor.createIfNotPresent(lock.id());

            boolean locked = lock.lock();
            if (!locked) {
                throw new LockAccessException("Lock has been occupied");
            }
        } catch (Exception e) {
            throw new LockAccessException(e);
        }
    }

    public String getMysqlTableName() {
        return mysqlTableName;
    }

    public void setMysqlTableName(String mysqlTableName) {
        this.mysqlTableName = mysqlTableName;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

}

与墨瑜小姐姐互动,为她打call——>点击进去墨瑜晒码

更多小姐姐,点击进入查看代码

有被代码耽误的钉钉吃货程序媛,写代码写到忘记吃饭的——采霜
她急需能把她从代码中叫醒去吃饭的小伙伴,赶紧勾搭;
http://yq.aliyun.com/roundtable/126499/answer/170319#visit170319


有以代码为乐的饿了么大前端打(bei)杂(guo)工程师——敖天羽
http://yq.aliyun.com/roundtable/126499/answer/170299#visit170299


还有全栈美女工程师——前端后端一锅端的——墨瑜女神
http://yq.aliyun.com/roundtable/126499/answer/170316#visit170316


还有阿里云活好码赞,像男人一样活着的技术专家小姐姐——清宵http://yq.aliyun.com/roundtable/126499/answer/170296#visit170296


和天猫Java程序媛,happy(被)来(逼)的——采月
http://yq.aliyun.com/roundtable/126499/answer/170354#visit170354


阿里口碑 搜索推荐组的JAVA女工程师——妍霏
http://yq.aliyun.com/roundtable/126499/answer/214059#visit214059


阿里口碑基础平台技术部的工程师-衔远,一个技术与马甲线并存的mm
http://yq.aliyun.com/roundtable/126499/answer/248798#visit248798

云栖社区聚能聊专家,具有知性温婉气质的——海洋的云http://yq.aliyun.com/roundtable/126499/answer/170290#visit170290

福州市五佰网络科技有限公司的Java女工程师——江小白太白
http://yq.aliyun.com/roundtable/126499/answer/189332#visit189332


在下*的程序媛——cn_suqingnian
http://yq.aliyun.com/roundtable/126499/answer/170309#visit170309


交通银行的前端女工程师——昆0830
http://yq.aliyun.com/roundtable/126499/answer/201979#visit201979

上一篇:【程序媛晒83行代码】阿里口碑精致程序媛, 搜索推荐组的JAVA女工程师


下一篇:【程序媛晒83行代码】单身狗汪化翻译机,把你的话都变成汪星语