This repository was archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from crossoverJie/use-mq
✨ Use MQ
- Loading branch information
Showing
18 changed files
with
478 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
SSM-WEB/src/main/java/com/crossoverJie/kafka/Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.crossoverJie.kafka; | ||
|
||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
/** Kafka生产者 | ||
* @author crossoverJie | ||
*/ | ||
public class Producer { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(Producer.class); | ||
|
||
/** | ||
* 消费配置文件 | ||
*/ | ||
private static String consumerProPath; | ||
|
||
public static void main(String[] args) throws IOException { | ||
// set up the producer | ||
consumerProPath = System.getProperty("product_path"); | ||
KafkaProducer<String, String> producer = null; | ||
try { | ||
FileInputStream inputStream = new FileInputStream(new File(consumerProPath)); | ||
Properties properties = new Properties(); | ||
properties.load(inputStream); | ||
producer = new KafkaProducer<String, String>(properties); | ||
|
||
} catch (IOException e) { | ||
LOGGER.error("load config error", e); | ||
} | ||
|
||
try { | ||
// send lots of messages | ||
for (int i=0 ;i<100 ; i++){ | ||
producer.send(new ProducerRecord<String, String>( | ||
"topic_optimization", i+"", i+"")); | ||
|
||
} | ||
} catch (Throwable throwable) { | ||
System.out.printf("%s", throwable.getStackTrace()); | ||
} finally { | ||
producer.close(); | ||
} | ||
|
||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
SSM-WEB/src/main/java/com/crossoverJie/kafka/official/KafkaOfficialConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.crossoverJie.kafka.official; | ||
|
||
import com.crossoverJie.util.StringUtil; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Function:kafka官方消费 | ||
* | ||
* @author crossoverJie | ||
* Date: 2017/10/19 01:11 | ||
* @since JDK 1.8 | ||
*/ | ||
public class KafkaOfficialConsumer { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaOfficialConsumer.class); | ||
|
||
/** | ||
* 日志文件地址 | ||
*/ | ||
private static String logPath; | ||
|
||
/** | ||
* 主题名称 | ||
*/ | ||
private static String topic; | ||
|
||
/** | ||
* 消费配置文件 | ||
*/ | ||
private static String consumerProPath ; | ||
|
||
|
||
/** | ||
* 初始化参数校验 | ||
* @return | ||
*/ | ||
private static boolean initCheck() { | ||
topic = System.getProperty("topic") ; | ||
logPath = System.getProperty("log_path") ; | ||
consumerProPath = System.getProperty("consumer_pro_path") ; | ||
if (StringUtil.isEmpty(topic) || logPath.isEmpty()) { | ||
LOGGER.error("system property topic ,consumer_pro_path, log_path is required !"); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 初始化kafka配置 | ||
* @return | ||
*/ | ||
private static KafkaConsumer<String, String> initKafkaConsumer() { | ||
KafkaConsumer<String, String> consumer = null; | ||
try { | ||
FileInputStream inputStream = new FileInputStream(new File(consumerProPath)) ; | ||
Properties properties = new Properties(); | ||
properties.load(inputStream); | ||
consumer = new KafkaConsumer<String, String>(properties); | ||
consumer.subscribe(Arrays.asList(topic)); | ||
|
||
} catch (IOException e) { | ||
LOGGER.error("加载consumer.props文件出错", e); | ||
} | ||
return consumer; | ||
} | ||
|
||
public static void main(String[] args) { | ||
if (initCheck()){ | ||
return; | ||
} | ||
|
||
int totalCount = 0 ; | ||
long totalMin = 0L ; | ||
int count = 0; | ||
KafkaConsumer<String, String> consumer = initKafkaConsumer(); | ||
|
||
long startTime = System.currentTimeMillis() ; | ||
//消费消息 | ||
while (true) { | ||
ConsumerRecords<String, String> records = consumer.poll(200); | ||
if (records.count() <= 0){ | ||
continue ; | ||
} | ||
LOGGER.debug("本次获取:"+records.count()); | ||
count += records.count() ; | ||
|
||
long endTime = System.currentTimeMillis() ; | ||
LOGGER.debug("count=" +count) ; | ||
if (count >= 10000 ){ | ||
totalCount += count ; | ||
LOGGER.info("this consumer {} record,use {} milliseconds",count,endTime-startTime); | ||
totalMin += (endTime-startTime) ; | ||
startTime = System.currentTimeMillis() ; | ||
count = 0 ; | ||
} | ||
LOGGER.debug("end totalCount={},min={}",totalCount,totalMin); | ||
|
||
/*for (ConsumerRecord<String, String> record : records) { | ||
record.value() ; | ||
JsonNode msg = null; | ||
try { | ||
msg = mapper.readTree(record.value()); | ||
} catch (IOException e) { | ||
LOGGER.error("消费消息出错", e); | ||
} | ||
LOGGER.info("kafka receive = "+msg.toString()); | ||
}*/ | ||
|
||
|
||
} | ||
} | ||
} |
Oops, something went wrong.