以下是上述情况的示例实现。请注意,即使一些其他人已经回复提到了没有Guava缓存也可以实现,但我认为Nirmalaya请求使用它可能有一个有效的原因。我能想到的一个原因是将缓存溢出到存储设备或数据库中,以节省运行时内存。
employee-records.xml
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="1">
<name>Thomas</name>
</Employee>
<Employee id="2">
<name>Lisa</name>
</Employee>
<Employee id="3">
<name>Ronald</name>
</Employee>
<Employee id="4">
<name>Erica</name>
</Employee>
</Employees>
Employee.java
package com.technoroy.examples.guava;
class Employee {
private Integer id = null;
private String name = null;
public Employee() {
super();
}
public Employee(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
GuavaCacheProcessor.java
package com.technoroy.examples.guava;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
public class GuavaCacheProcessor {
private final static BlockingQueue<Integer> notificationQueue = new LinkedBlockingQueue<>();
public static void main(String... arguments) {
Runnable xmlProcessor = new Runnable() {
public void run() {
parseDataFile();
}
};
Runnable mdlGenerator = new Runnable() {
public void run() {
try {
while (true) {
Integer id = notificationQueue.take();
Employee record = ApplicationCacheUtil.getRecord(id);
generateContent(record);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(xmlProcessor);
executorService.submit(mdlGenerator);
}
public static void generateContent(Employee employee) {
System.out.println(employee);
}
public static void parseDataFile() {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
InputStream dataInputStream = GuavaCacheProcessor.class.getResourceAsStream("employee-records.xml");
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
saxParser.parse(dataInputStream, new DefaultHandler() {
private Employee employee = null;
private StringBuilder elementValue = null;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("Employee")) {
employee = new Employee();
String id = attributes.getValue("id");
if (id.matches("-?\\d+(\\.\\d+)?")) {
employee.setId(Integer.valueOf(id));
}
}
elementValue = new StringBuilder();
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (elementValue != null) {
elementValue.append(new String(ch, start, length));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("name")) {
if (employee != null && elementValue != null) {
employee.setName(elementValue.toString());
}
} else if (qName.equalsIgnoreCase("Employee")) {
ApplicationCacheUtil.putRecord(employee.getId(), employee);
try {
notificationQueue.put(employee.getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
elementValue = null;
}
});
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}
class ApplicationCacheUtil {
private static Cache<Integer, Employee> cache = CacheBuilder.newBuilder().build();
public static Cache<Integer, Employee> getCache() {
return cache;
}
public static void putRecord(Integer key, Employee value) {
cache.put(key, value);
}
public static Employee getRecord(Integer key) {
return cache.getIfPresent(key);
}
}
XMLProcessor
将结果发布到队列中,然后你的CacheProcessor
监听队列并处理项目,CacheService
和MDLService
同理。 - Mickael