Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop
Project
Project
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
曲欣亮
shop
Commits
e7db5fc0
Commit
e7db5fc0
authored
Oct 06, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
04b53662
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
0 deletions
+97
-0
AliyunLogProducerWithFuture.java
...com/egolm/shop/aliyunLog/AliyunLogProducerWithFuture.java
+71
-0
AliyunLogUtil.java
src/main/java/com/egolm/shop/aliyunLog/AliyunLogUtil.java
+26
-0
No files found.
src/main/java/com/egolm/shop/aliyunLog/AliyunLogProducerWithFuture.java
0 → 100644
View file @
e7db5fc0
package
com
.
egolm
.
shop
.
aliyunLog
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.atomic.AtomicLong
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.aliyun.openservices.aliyun.log.producer.Producer
;
import
com.aliyun.openservices.aliyun.log.producer.Result
;
import
com.aliyun.openservices.aliyun.log.producer.errors.ResultFailedException
;
import
com.aliyun.openservices.log.common.LogItem
;
import
com.google.common.util.concurrent.FutureCallback
;
import
com.google.common.util.concurrent.Futures
;
import
com.google.common.util.concurrent.ListenableFuture
;
public
class
AliyunLogProducerWithFuture
{
private
static
final
ExecutorService
EXECUTOR_SERVICE
=
Executors
.
newCachedThreadPool
();
private
static
final
Producer
producer
=
AliyunLogUtil
.
createProducer
();
private
static
final
String
project
=
System
.
getenv
(
"PROJECT"
);
private
static
final
String
logStore
=
System
.
getenv
(
"LOG_STORE"
);
public
static
void
sendLog
(
String
logText
)
{
try
{
final
AtomicLong
completed
=
new
AtomicLong
(
0
);
List
<
LogItem
>
logItems
=
new
ArrayList
<
LogItem
>();
ListenableFuture
<
Result
>
future
=
producer
.
send
(
project
,
logStore
,
logItems
);
Futures
.
addCallback
(
future
,
new
SampleFutureCallback
(
project
,
logStore
,
completed
),
EXECUTOR_SERVICE
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
static
final
class
SampleFutureCallback
implements
FutureCallback
<
Result
>
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
SampleFutureCallback
.
class
);
private
final
String
project
;
private
final
String
logStore
;
private
final
AtomicLong
completed
;
SampleFutureCallback
(
String
project
,
String
logStore
,
AtomicLong
completed
)
{
this
.
project
=
project
;
this
.
logStore
=
logStore
;
this
.
completed
=
completed
;
}
@Override
public
void
onSuccess
(
Result
result
)
{
LOGGER
.
info
(
"Send logs successfully."
);
completed
.
getAndIncrement
();
}
@Override
public
void
onFailure
(
Throwable
t
)
{
if
(
t
instanceof
ResultFailedException
)
{
Result
result
=
((
ResultFailedException
)
t
).
getResult
();
LOGGER
.
error
(
"Failed to send logs, project={}, logStore={}, result={}"
,
project
,
logStore
,
result
);
}
else
{
LOGGER
.
error
(
"Failed to send log, e="
,
t
);
}
completed
.
getAndIncrement
();
}
}
}
src/main/java/com/egolm/shop/aliyunLog/AliyunLogUtil.java
0 → 100644
View file @
e7db5fc0
package
com
.
egolm
.
shop
.
aliyunLog
;
import
com.aliyun.openservices.aliyun.log.producer.LogProducer
;
import
com.aliyun.openservices.aliyun.log.producer.Producer
;
import
com.aliyun.openservices.aliyun.log.producer.ProducerConfig
;
import
com.aliyun.openservices.aliyun.log.producer.ProjectConfig
;
public
class
AliyunLogUtil
{
public
static
Producer
createProducer
()
{
ProducerConfig
producerConfig
=
new
ProducerConfig
();
Producer
producer
=
new
LogProducer
(
producerConfig
);
producer
.
putProjectConfig
(
buildProjectConfig
());
return
producer
;
}
private
static
ProjectConfig
buildProjectConfig
()
{
String
project
=
System
.
getenv
(
"PROJECT"
);
String
endpoint
=
System
.
getenv
(
"ENDPOINT"
);
String
accessKeyId
=
System
.
getenv
(
"ACCESS_KEY_ID"
);
String
accessKeySecret
=
System
.
getenv
(
"ACCESS_KEY_SECRET"
);
return
new
ProjectConfig
(
project
,
endpoint
,
accessKeyId
,
accessKeySecret
);
}
}
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