Toggle navigation
Toggle navigation
This project
Loading...
Sign in
YOHOBUY
/
yohobuy-union
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
ping
9 years ago
Commit
1319f05695d571bf42927ae40f28b28a5fea313c
1 parent
ce3e19f2
update write file
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
0 deletions
server/src/main/java/com/yoho/activity/thread/DrawServiceThread.java
web/src/main/resources/META-INF/spring/spring-mvc.xml
server/src/main/java/com/yoho/activity/thread/DrawServiceThread.java
0 → 100644
View file @
1319f05
package
com
.
yoho
.
activity
.
thread
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
import
javax.annotation.Resource
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.alibaba.fastjson.JSONObject
;
import
com.yoho.core.cache.CacheClient
;
import
com.yoho.core.redis.YHValueOperations
;
public
class
DrawServiceThread
{
static
Logger
log
=
LoggerFactory
.
getLogger
(
DrawServiceThread
.
class
);
private
static
final
String
DRAW_USER_LIST_KEY
=
"DRAW_USER_LIST_KEY"
;
private
static
final
String
DRAW_USER_LIST_MEM_KEY
=
"DRAW_USER_LIST_MEM_KEY"
;
@Resource
CacheClient
cacheClient
;
@Resource
YHValueOperations
<
String
,
String
>
valueOperations
;
String
fileName
=
"/Data/logs/union/redis.txt"
;
// String fileName = "D:/redis.txt";
/**
* 启动一个定时任务,来读取元宵节抽签活动的
*/
public
DrawServiceThread
()
{
//tomcat启动后,延迟一分钟,每个一个小时写一次文件
Executors
.
newScheduledThreadPool
(
1
).
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
log
.
info
(
"enter DrawServiceThread"
);
//cong redis中获取数据
String
arr
=
valueOperations
.
get
(
DRAW_USER_LIST_KEY
);
Map
<
String
,
JSONObject
>
maps
=
null
;
if
(
StringUtils
.
isEmpty
(
arr
))
{
maps
=
new
HashMap
<
String
,
JSONObject
>();
}
else
{
maps
=
(
Map
)
JSONObject
.
parse
(
arr
);
}
log
.
debug
(
"DrawServiceThread query redis data total is {}"
,
maps
.
size
());
//从mem里面获取
Map
<
String
,
String
>
memMap
=
cacheClient
.
get
(
DRAW_USER_LIST_MEM_KEY
,
Map
.
class
);
if
(
memMap
==
null
)
{
memMap
=
new
HashMap
<
String
,
String
>();
}
log
.
debug
(
"DrawServiceThread query mem data total is {}"
,
memMap
.
size
());
List
<
String
>
l
=
new
ArrayList
<
String
>();
for
(
Entry
<
String
,
JSONObject
>
entry
:
maps
.
entrySet
())
{
if
(
StringUtils
.
isNotEmpty
(
memMap
.
get
(
entry
.
getKey
())))
{
//该用户已经保存过文件,则不在进行重复写
continue
;
}
l
.
add
(
entry
.
getValue
().
toJSONString
());
try
{
FileUtils
.
writeLines
(
new
File
(
fileName
),
"UTF-8"
,
l
,
true
);
log
.
debug
(
"DrawServiceThread success write file with key={}, value={}"
,
entry
.
getKey
(),
entry
.
getValue
());
}
catch
(
IOException
e
)
{
log
.
error
(
"write file error with key={}, value={}"
,
entry
.
getKey
(),
entry
.
getValue
(),
e
);
}
//往mem里面记录,该用户已经写过文件的标记
memMap
.
put
(
entry
.
getKey
(),
"1"
);
}
cacheClient
.
set
(
DRAW_USER_LIST_MEM_KEY
,
30
*
24
*
60
*
60
,
memMap
);
log
.
info
(
"success out DrawServiceThread"
);
}
},
1
,
60
,
TimeUnit
.
MINUTES
);
}
}
...
...
web/src/main/resources/META-INF/spring/spring-mvc.xml
View file @
1319f05
...
...
@@ -19,6 +19,9 @@
<constructor-arg
name=
"memcachedServers"
value=
"${cache.servers.memcached.address}"
/>
</bean>
<!-- 元宵节抽签活动写文件 -->
<bean
class=
"com.yoho.activity.thread.DrawServiceThread"
/>
<mvc:annotation-driven
/>
<!-- 装在properties文件 -->
<context:property-placeholder
location=
"classpath*:config.properties"
/>
...
...
Please
register
or
login
to post a comment