Last active
September 9, 2019 13:41
-
-
Save zolotyh/b892e4c58703bca9f2ea126f692aa5ac to your computer and use it in GitHub Desktop.
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
package daoservice; | |
import java.util.function.Consumer; | |
@SuppressWarnings("unused") | |
public final class DaoService { | |
private ConnectionPool pool = new ConnectionPool(1); | |
public void updateTimestamp(long id) { | |
perform(conn -> { | |
if (!conn.recordExists(id)) | |
create(id); | |
conn.updateTimestamp(id, System.currentTimeMillis()); | |
}); | |
} | |
private void create(long id) { | |
perform(conn -> { | |
try { | |
conn.createRecord(id); | |
} catch (DuplicateKeyException ignored) { | |
} | |
}); | |
} | |
private void perform(Consumer<Connection> c) { | |
Connection conn = pool.borrowConnection(); | |
try { | |
c.accept(conn); | |
} | |
finally { | |
pool.returnConnection(conn); | |
} | |
} | |
} |
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
package xmlservice; | |
import lombok.extern.slf4j.Slf4j; | |
import org.w3c.dom.Node; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
@SuppressWarnings("unused") | |
@Slf4j | |
public class XmlService { | |
static final class Request { | |
String originalXmlRequest; | |
Node root; | |
} | |
private final ExecutorService executor = Executors.newFixedThreadPool(1); | |
public void enqueue(Request req) { | |
log.info("Got XML: {}", req.originalXmlRequest); | |
executor.execute(() -> process(req.root)); | |
} | |
private void process(Node root) { | |
// do stuff on parsed xml DOM tree starting on 'root' | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment