Slack Java API and the Data Non Problem
Recently I had the "joy" to work with the Slack Java API. I felt that this Java API is tedious and an example of an API which create busy work for little benefit. Let me show first how the API is used:
var slack = Slack.getInstance();
var methods = slack.methods();
var channeId = "C424242";
var request = ChatPostMessageRequest.builder()
.channel(channeId)
.text(":wave: Hi from a bot written in Java!")
.build();
var response = methods.chatPostMessage(request);
Keep Time Explicit
In a lot of software is operations are depending on time. For example creating invoices, statistics, showing the latest news so on so forth. In this blog post, I use an example summing up bought items in the last week. We might have multiple layers in our system, like database access, places to do extra processing, and a web interface. I’m using C# here, but it applies to most mainstream languages. As you can see, to get the right database records the cure uses the current time.
public class Purchases
{
public IList<Purchase> LastWeeksPurchases()
{
// Assume more complex queries, let's say joins with users, applied coupons, discounts at the time and god knows what
return Database.InTransaction(conn => conn.Query<Purchase>(@"select * from purchases
where dateTime < current_timestamp
and (current_timestamp + INTERVAL '-7 day') < dateTime
order by dateTime").ToList());
}
}
C# Loves Code Patterns
This post is part of the third annual C# Advent. Check out the home page for up to 50 C# blog posts in December 2019! Thanks, Matthew D. Groves for organizing it.
You might recall that LINQ has two forms. The first way is to call LINQ methods and the second is the special LINQ syntax. Here’s an example of the LINQ syntax.
Synchronizing File Writes
Assuming we have a simple task to write events to a file, one event after another. Events can be written from multiple threads.
public interface EventLog {
public final class Event{
public final UUID id;
public final ZonedDateTime created;
public final String message;
public Event(UUID id, ZonedDateTime created, String message) {
this.id = id;
this.created = created;
this.message = message;
}
}
void recordEvent(Event event);
// Null Logger doing nothing to have a base line
EventLog NULL_LOGGER = new NullLogger();
class NullLogger implements EventLog {
@Override
public void recordEvent(Event event) {
// do nothing;
}
@Override
public void close() throws Exception {
}
}
}
FileChannel Closes When Interrupted
Recently I worked on some threaded IO where a control thread interrupts the IO threads once in a while.
Periodically the app started to fail with a java.nio.channels.ClosedChannelException
.