Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nouiWithSpringMVC
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gechengyang
nouiWithSpringMVC
Commits
05ced278
Commit
05ced278
authored
Sep 13, 2020
by
WeiCong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加拨号(cnt表使用)缓存器使用
parent
739cf430
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
0 deletions
+120
-0
CacheCounter.java
src/main/java/org/sss/module/pojo/CacheCounter.java
+120
-0
No files found.
src/main/java/org/sss/module/pojo/CacheCounter.java
0 → 100644
View file @
05ced278
package
org
.
sss
.
module
.
pojo
;
import
log.Log
;
import
log.LogFactory
;
import
org.apache.commons.dbutils.DbUtils
;
import
org.hibernate.engine.spi.SessionImplementor
;
import
org.sss.module.hibernate.HibernateUtils
;
import
org.sss.presentation.noui.util.NoUiUtils
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
CacheCounter
{
static
final
Log
log
=
LogFactory
.
getLog
(
CacheCounter
.
class
);
private
static
final
Map
<
String
,
Counter
>
CACHE
=
Collections
.
synchronizedMap
(
new
HashMap
());
private
static
final
CacheCounter
INSTANCE
=
new
CacheCounter
();
private
static
final
String
INSERTCNT
=
"INSERT INTO cnt (nam,val,stp) VALUES (?,?,?)"
;
private
static
final
String
UPDATECNT
=
"UPDATE cnt SET val=? WHERE nam= ?"
;
public
static
int
count
(
String
counterName
)
{
Map
<
String
,
Counter
>
cache
=
CACHE
;
Counter
count
=
cache
.
get
(
counterName
);
if
(
count
==
null
)
{
synchronized
(
CacheCounter
.
class
)
{
count
=
cache
.
get
(
counterName
);
if
(
count
==
null
)
{
count
=
INSTANCE
.
new
Counter
(
counterName
);
cache
.
put
(
counterName
,
count
);
}
}
}
return
count
.
getCurCount
();
}
public
class
Counter
{
AtomicInteger
curCount
=
new
AtomicInteger
();
volatile
int
stopCount
;
String
counterName
;
Object
lock
=
new
Object
();
public
Counter
(
String
counterName
)
{
this
.
counterName
=
counterName
;
loadCnt
();
}
boolean
needUpdate
(
int
cur
)
{
return
(
cur
>=
stopCount
)
&&
(
cur
!=
0
);
}
public
int
getCurCount
()
{
int
cur
=
curCount
.
getAndIncrement
();
if
(
needUpdate
(
cur
))
{
synchronized
(
lock
)
{
cur
=
curCount
.
getAndIncrement
();
if
(
needUpdate
(
cur
))
{
loadCnt
();
cur
=
curCount
.
getAndIncrement
();
}
}
}
return
cur
;
}
private
int
loadCnt
()
{
int
countValue
=
0
;
int
endCount
=
0
;
SessionImplementor
session
=
null
;
Connection
conn
=
null
;
PreparedStatement
stmt
=
null
;
ResultSet
rs
=
null
;
try
{
session
=
(
SessionImplementor
)
HibernateUtils
.
openSession
(
null
);
conn
=
session
.
connection
();
if
(
conn
.
getAutoCommit
())
{
conn
.
setAutoCommit
(
false
);
}
String
sql
=
DatabaseUtils
.
getCounterSelectSQL
(
conn
,
counterName
);
boolean
updatable
=
sql
.
endsWith
(
"UPDATE"
);
stmt
=
conn
.
prepareStatement
(
sql
,
ResultSet
.
TYPE_FORWARD_ONLY
,
updatable
?
ResultSet
.
CONCUR_UPDATABLE
:
ResultSet
.
CONCUR_READ_ONLY
);
rs
=
stmt
.
executeQuery
();
if
(
rs
.
next
())
{
countValue
=
rs
.
getInt
(
1
);
endCount
=
countValue
+
NoUiUtils
.
STP
;
if
(
updatable
)
{
rs
.
updateInt
(
1
,
endCount
);
rs
.
updateRow
();
}
else
{
DbUtils
.
closeQuietly
((
Connection
)
null
,
stmt
,
rs
);
stmt
=
conn
.
prepareStatement
(
UPDATECNT
);
stmt
.
setInt
(
1
,
endCount
);
stmt
.
setString
(
2
,
counterName
);
stmt
.
execute
();
}
}
else
{
countValue
=
0
;
endCount
=
NoUiUtils
.
STP
;
DbUtils
.
closeQuietly
((
Connection
)
null
,
stmt
,
rs
);
stmt
=
conn
.
prepareStatement
(
INSERTCNT
);
stmt
.
setString
(
1
,
counterName
);
stmt
.
setInt
(
2
,
endCount
);
stmt
.
setInt
(
3
,
1
);
stmt
.
execute
();
}
conn
.
commit
();
}
catch
(
Exception
e
)
{
log
.
error
(
"loadCnt error."
,
e
);
}
finally
{
DbUtils
.
closeQuietly
((
Connection
)
null
,
stmt
,
rs
);
curCount
.
set
(
countValue
);
stopCount
=
endCount
;
}
return
countValue
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment