Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
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
沈俊林
webmagic
Commits
74962d69
Commit
74962d69
authored
Feb 13, 2015
by
edwardsbean
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bug:MultiPagePipeline and DoubleKeyMap concurrent bug
parent
6b9d21fc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
36 deletions
+47
-36
MultiPagePipeline.java
...ava/us/codecraft/webmagic/pipeline/MultiPagePipeline.java
+44
-34
DoubleKeyMap.java
...c/main/java/us/codecraft/webmagic/utils/DoubleKeyMap.java
+3
-2
No files found.
webmagic-extension/src/main/java/us/codecraft/webmagic/pipeline/MultiPagePipeline.java
View file @
74962d69
...
...
@@ -36,9 +36,15 @@ public class MultiPagePipeline implements Pipeline {
private
void
handleObject
(
Iterator
<
Map
.
Entry
<
String
,
Object
>>
iterator
)
{
Map
.
Entry
<
String
,
Object
>
objectEntry
=
iterator
.
next
();
Object
o
=
objectEntry
.
getValue
();
//需要拼凑
if
(
o
instanceof
MultiPageModel
)
{
MultiPageModel
multiPageModel
=
(
MultiPageModel
)
o
;
//这次处理的部分,设置为完成
pageMap
.
put
(
multiPageModel
.
getPageKey
(),
multiPageModel
.
getPage
(),
Boolean
.
FALSE
);
//每个key单独加锁
synchronized
(
pageMap
.
get
(
multiPageModel
.
getPageKey
()))
{
pageMap
.
put
(
multiPageModel
.
getPageKey
(),
multiPageModel
.
getPage
(),
Boolean
.
TRUE
);
//其他需要拼凑的部分
if
(
multiPageModel
.
getOtherPages
()
!=
null
)
{
for
(
String
otherPage
:
multiPageModel
.
getOtherPages
())
{
Boolean
aBoolean
=
pageMap
.
get
(
multiPageModel
.
getPageKey
(),
otherPage
);
...
...
@@ -53,6 +59,7 @@ public class MultiPagePipeline implements Pipeline {
if
(
booleanMap
==
null
)
{
return
;
}
// /过滤,这次完成的page item中,还未拼凑完整的item,不进入下一个pipeline
for
(
Map
.
Entry
<
String
,
Boolean
>
stringBooleanEntry
:
booleanMap
.
entrySet
())
{
if
(!
stringBooleanEntry
.
getValue
())
{
iterator
.
remove
();
...
...
@@ -74,6 +81,7 @@ public class MultiPagePipeline implements Pipeline {
}
}
});
// 合并
MultiPageModel
value
=
entryList
.
get
(
0
).
getValue
();
for
(
int
i
=
1
;
i
<
entryList
.
size
();
i
++)
{
value
=
value
.
combine
(
entryList
.
get
(
i
).
getValue
());
...
...
@@ -83,4 +91,6 @@ public class MultiPagePipeline implements Pipeline {
}
}
}
}
webmagic-extension/src/main/java/us/codecraft/webmagic/utils/DoubleKeyMap.java
View file @
74962d69
...
...
@@ -75,8 +75,9 @@ public class DoubleKeyMap<K1, K2, V> extends MultiKeyMapBase {
* @param value
* @return value
*/
public
V
put
(
K1
key1
,
K2
key2
,
V
value
)
{
public
synchronized
V
put
(
K1
key1
,
K2
key2
,
V
value
)
{
if
(
map
.
get
(
key1
)
==
null
)
{
//不加锁的话,多个线程有可能都会执行到这里
map
.
put
(
key1
,
this
.<
K2
,
V
>
newMap
());
}
return
get
(
key1
).
put
(
key2
,
value
);
...
...
@@ -87,7 +88,7 @@ public class DoubleKeyMap<K1, K2, V> extends MultiKeyMapBase {
* @param key2
* @return value
*/
public
V
remove
(
K1
key1
,
K2
key2
)
{
public
synchronized
V
remove
(
K1
key1
,
K2
key2
)
{
if
(
get
(
key1
)
==
null
)
{
return
null
;
}
...
...
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