INTSAMPLES-147: Add Splunk example
JIRA: https://jira.spring.io/browse/INTSAMPLES-147
This commit is contained in:
committed by
Artem Bilan
parent
8f7fb189c0
commit
b9697909d0
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.samples.splunk;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.integration.samples.splunk.event.OrderEvent;
|
||||
import org.springframework.integration.splunk.event.SplunkEvent;
|
||||
import org.springframework.integration.splunk.support.SplunkServer;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
* @author Filippo Balicchia
|
||||
* @since 4.2
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ImportResource("si-splunk-example-context.xml")
|
||||
public class Application {
|
||||
|
||||
@Value("${splunk.host}")
|
||||
private String host;
|
||||
@Value("${splunk.port}")
|
||||
private String port;
|
||||
@Value("${splunk.username}")
|
||||
private String username;
|
||||
@Value("${splunk.password}")
|
||||
private String password;
|
||||
@Value("${splunk.owner}")
|
||||
private String owner;
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
|
||||
MessageChannel channelRestOutput = context.getBean("toSplunk", MessageChannel.class);
|
||||
sendWithRest(channelRestOutput);
|
||||
System.out.println("Consumer");
|
||||
|
||||
}
|
||||
|
||||
private static void sendWithRest(MessageChannel channel) {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
channel.send(MessageBuilder.withPayload(createEvent()).build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static SplunkEvent createEvent(){
|
||||
int nextInt = ThreadLocalRandom.current().nextInt(1, 1000);
|
||||
OrderEvent sd = new OrderEvent();
|
||||
sd.setEan(String.valueOf (nextInt));
|
||||
sd.setEmailuser("mail@gmail.com");
|
||||
sd.setOrderNumber("21501001010101");
|
||||
return sd;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SplunkServer splunkServerRef() {
|
||||
SplunkServer splunkServer = new SplunkServer();
|
||||
splunkServer.setPort(Integer.valueOf(port));
|
||||
splunkServer.setHost(host);
|
||||
splunkServer.setUsername(username);
|
||||
splunkServer.setOwner(owner);
|
||||
splunkServer.setPassword(password);
|
||||
return splunkServer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.samples.splunk.event;
|
||||
|
||||
import org.springframework.integration.splunk.event.SplunkEvent;
|
||||
|
||||
/**
|
||||
* @author Filippo Balicchia
|
||||
* @since 4.2
|
||||
*/
|
||||
public class OrderEvent extends SplunkEvent {
|
||||
|
||||
private static final long serialVersionUID = 4203808769092252241L;
|
||||
|
||||
public static String ORDER_NUMBER = "order_number";
|
||||
|
||||
public static String EAN_ITEM = "ean";
|
||||
|
||||
public static String EMAIL_USER = "email";
|
||||
|
||||
public OrderEvent() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setOrderNumber(String orderNumber) {
|
||||
addPair(ORDER_NUMBER, orderNumber);
|
||||
}
|
||||
|
||||
public void setEan(String eanNumber) {
|
||||
addPair(EAN_ITEM, eanNumber);
|
||||
}
|
||||
|
||||
public void setEmailuser(String email) {
|
||||
addPair(EMAIL_USER, email);
|
||||
}
|
||||
|
||||
}
|
||||
6
basic/splunk/src/main/resources/application.yml
Normal file
6
basic/splunk/src/main/resources/application.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
splunk:
|
||||
host: localhost
|
||||
port: 8089
|
||||
username: admin
|
||||
password: admin
|
||||
owner: admin
|
||||
15
basic/splunk/src/main/resources/banner.txt
Normal file
15
basic/splunk/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
${AnsiColor.GREEN} _____ _ _____ _ _ _
|
||||
${AnsiColor.GREEN} / ____| (_) |_ _| | | | | (_)
|
||||
${AnsiColor.GREEN} | (___ _ __ _ __ _ _ __ __ _ | | _ __ | |_ ___ __ _ _ __ __ _| |_ _ ___ _ __
|
||||
${AnsiColor.GREEN} \___ \| '_ \| '__| | '_ \ / _` | | | | '_ \| __/ _ \/ _` | '__/ _` | __| |/ _ \| '_ \
|
||||
${AnsiColor.GREEN} ____) | |_) | | | | | | | (_| | _| |_| | | | || __/ (_| | | | (_| | |_| | (_) | | | |
|
||||
${AnsiColor.GREEN} |_____/| .__/|_| |_|_| |_|\__, | |_____|_| |_|\__\___|\__, |_| \__,_|\__|_|\___/|_| |_|
|
||||
${AnsiColor.GREEN} | | __/ | __/ | ${AnsiColor.BRIGHT_BLUE} ____________
|
||||
${AnsiColor.GREEN} _____|_| _ |___/ _____ |___/ _ ${AnsiColor.BRIGHT_BLUE} \ \ \ \ \ \ \
|
||||
${AnsiColor.GREEN} / ____| | | | | / ____| | | ${AnsiColor.BRIGHT_BLUE} \ \ \ \ \ \ \
|
||||
${AnsiColor.GREEN} | (___ _ __ | |_ _ _ __ | | __ | (___ __ _ _ __ ___ _ __ | | ___ ${AnsiColor.BRIGHT_BLUE} \ \ \ \ \ \ \
|
||||
${AnsiColor.GREEN} \___ \| '_ \| | | | | '_ \| |/ / \___ \ / _` | '_ ` _ \| '_ \| |/ _ \${AnsiColor.BRIGHT_BLUE} / / / / / / /
|
||||
${AnsiColor.GREEN} ____) | |_) | | |_| | | | | < ____) | (_| | | | | | | |_) | | __/${AnsiColor.BRIGHT_BLUE} / / / / / / /
|
||||
${AnsiColor.GREEN} |_____/| .__/|_|\__,_|_| |_|_|\_\ |_____/ \__,_|_| |_| |_| .__/|_|\___|${AnsiColor.BRIGHT_BLUE} / / / / / / /
|
||||
${AnsiColor.GREEN} ======| |===============================================| |===========
|
||||
${AnsiColor.GREEN} |_| |_|${AnsiColor.DEFAULT}
|
||||
14
basic/splunk/src/main/resources/logback.xml
Normal file
14
basic/splunk/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type
|
||||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:stream="http://www.springframework.org/schema/integration/stream"
|
||||
xmlns:int-splunk="http://www.springframework.org/schema/integration/splunk"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/splunk http://www.springframework.org/schema/integration/splunk/spring-integration-splunk.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<int:channel id="toSplunk" />
|
||||
|
||||
<int-splunk:outbound-channel-adapter
|
||||
id="splunkOutboundChannelAdapter" auto-startup="true" order="1"
|
||||
channel="toSplunk" splunk-server-ref="splunkServerRef" source-type="customMessage"
|
||||
source="tinyMain">
|
||||
<int-splunk:submit-writer index="tinyindex" />
|
||||
</int-splunk:outbound-channel-adapter>
|
||||
|
||||
<!-- Inbound -->
|
||||
|
||||
<int:channel id="inputFromSplunk" />
|
||||
|
||||
<stream:stdout-channel-adapter id="stdout"
|
||||
channel="inputFromSplunk" append-newline="true" />
|
||||
|
||||
<int-splunk:inbound-channel-adapter
|
||||
id="splunkInboundChannelAdapter" auto-startup="true" search="search ean > 135"
|
||||
splunk-server-ref="splunkServerRef" channel="inputFromSplunk" mode="BLOCKING"
|
||||
earliest-time="-1d" latest-time="now" init-earliest-time="-1d"
|
||||
field-list="field1, field2">
|
||||
<int:poller fixed-rate="5000" />
|
||||
</int-splunk:inbound-channel-adapter>
|
||||
</beans>
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.samples.splunk;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Filippo Balicchia
|
||||
* @since 4.2
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@ClassRule
|
||||
public static SplunkWatcher splunkWatcher = new SplunkWatcher();
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.samples.splunk;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TestWatcher;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.springframework.integration.splunk.support.SplunkServer;
|
||||
import org.springframework.integration.splunk.support.SplunkServiceFactory;
|
||||
|
||||
import com.splunk.Service;
|
||||
|
||||
/**
|
||||
* @author Filippo Balicchia
|
||||
* @since 4.2
|
||||
*/
|
||||
public class SplunkWatcher extends TestWatcher {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SplunkWatcher.class);
|
||||
|
||||
private final int port;
|
||||
|
||||
public SplunkWatcher() {
|
||||
this(Service.DEFAULT_PORT);
|
||||
}
|
||||
|
||||
public SplunkWatcher(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
|
||||
SplunkServer splunkServer = new SplunkServer();
|
||||
splunkServer.setPassword("admin");
|
||||
splunkServer.setUsername("admin");
|
||||
SplunkServiceFactory splunkServiceFactory = new SplunkServiceFactory(splunkServer);
|
||||
splunkServiceFactory.getService().open(this.port);
|
||||
System.setProperty("splunk.port", "" + this.port);
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.warn(
|
||||
"Not executing tests because basic connectivity test failed");
|
||||
Assume.assumeNoException(e);
|
||||
}
|
||||
|
||||
return super.apply(base, description);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user