StateSet Embedded Commerce - Java
Local-first commerce engine for Java applications. Native JNI bindings to the Rust commerce core.
Installation
Maven
<dependency>
<groupId>com.stateset</groupId>
<artifactId>embedded</artifactId>
<version>0.7.10</version>
</dependency>
Gradle
implementation 'com.stateset:embedded:0.7.10'
Gradle (Kotlin DSL)
implementation("com.stateset:embedded:0.7.10")
Quick Start
import com.stateset.embedded.*;
public class Example {
public static void main(String[] args) {
try (Commerce commerce = new Commerce("commerce.db")) {
Customer customer = commerce.customers().create(
"alice@example.com",
"Alice",
"Smith"
);
System.out.println("Created customer: " + customer.getId());
Product product = commerce.products().create(
"WIDGET-001",
"Premium Widget",
29.99
);
System.out.println("Created product: " + product.getSku());
InventoryItem item = commerce.inventory().adjust(
product.getSku(),
100,
"Initial stock"
);
System.out.println("Stock level: " + item.getQuantityAvailable());
Cart cart = commerce.carts().create(customer.getId(), "USD");
cart = commerce.carts().addItem(
cart.getId(),
product.getSku(),
product.getName(),
2,
product.getBasePrice()
);
Order order = commerce.carts().checkout(cart.getId());
System.out.println("Order created: " + order.getId());
commerce.payments().recordPayment(
order.getId(),
order.getTotalAmount(),
"card",
"txn_123456"
);
SalesSummary summary = commerce.analytics().salesSummary(30);
System.out.println("Revenue (30 days): $" + summary.getTotalRevenue());
} catch (StateSetException e) {
System.err.println("Commerce error: " + e.getMessage());
}
}
}
API Reference
Commerce
The main entry point. Implements AutoCloseable for automatic resource management.
Commerce commerce = new Commerce("commerce.db");
commerce.customers() commerce.products() commerce.inventory() commerce.orders() commerce.carts() commerce.payments() commerce.returns() commerce.analytics()
commerce.close();
Customers
Customer customer = commerce.customers().create(
"email@example.com",
"FirstName",
"LastName"
);
Optional<Customer> found = commerce.customers().get(customer.getId());
List<Customer> customers = commerce.customers().list();
Products
Product product = commerce.products().create(
"SKU-001",
"Product Name",
99.99
);
Optional<Product> found = commerce.products().get("SKU-001");
List<Product> products = commerce.products().list();
Inventory
InventoryItem item = commerce.inventory().adjust(
"SKU-001",
50, "Restock from supplier"
);
Optional<InventoryItem> stock = commerce.inventory().get("SKU-001");
stock.ifPresent(i -> {
System.out.println("Available: " + i.getQuantityAvailable());
System.out.println("Reserved: " + i.getQuantityReserved());
});
commerce.inventory().reserve("SKU-001", 5);
commerce.inventory().release("SKU-001", 5);
Orders
Order order = commerce.orders().create(customer.getId(), "USD");
Optional<Order> found = commerce.orders().get(order.getId());
List<Order> orders = commerce.orders().list();
commerce.orders().updateStatus(order.getId(), "shipped");
Shopping Carts
Cart cart = commerce.carts().create(customerId, "USD");
cart = commerce.carts().addItem(
cart.getId(),
"SKU-001",
"Product Name",
2, 29.99 );
Optional<Cart> found = commerce.carts().get(cart.getId());
Order order = commerce.carts().checkout(cart.getId());
Payments
commerce.payments().recordPayment(
orderId,
amount,
"card", "txn_123456" );
Returns
ReturnRequest returnReq = commerce.returns().create(
orderId,
"Product arrived damaged",
Arrays.asList("item1", "item2") );
commerce.returns().process(returnReq.getId());
commerce.returns().refund(returnReq.getId(), 59.98);
Analytics
SalesSummary summary = commerce.analytics().salesSummary(30); SalesSummary allTime = commerce.analytics().salesSummary(0);
System.out.println("Orders: " + summary.getTotalOrders());
System.out.println("Revenue: $" + summary.getTotalRevenue());
System.out.println("AOV: $" + summary.getAverageOrderValue());
Framework Integration
Spring Boot
@Configuration
public class CommerceConfig {
@Bean(destroyMethod = "close")
public Commerce commerce() {
return new Commerce("commerce.db");
}
}
@Service
public class ProductService {
private final Commerce commerce;
public ProductService(Commerce commerce) {
this.commerce = commerce;
}
public Product createProduct(String sku, String name, double price) {
return commerce.products().create(sku, name, price);
}
public List<Product> getAllProducts() {
return commerce.products().list();
}
}
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;
@PostMapping
public Product create(@RequestBody CreateProductRequest req) {
return productService.createProduct(
req.getSku(),
req.getName(),
req.getPrice()
);
}
@GetMapping
public List<Product> list() {
return productService.getAllProducts();
}
}
Micronaut
@Factory
public class CommerceFactory {
@Singleton
@Bean(preDestroy = "close")
public Commerce commerce() {
return new Commerce("commerce.db");
}
}
@Controller("/api/orders")
public class OrderController {
private final Commerce commerce;
public OrderController(Commerce commerce) {
this.commerce = commerce;
}
@Get("/{id}")
public Optional<Order> get(String id) {
return commerce.orders().get(id);
}
@Get
public List<Order> list() {
return commerce.orders().list();
}
}
Quarkus
@ApplicationScoped
public class CommerceProducer {
private Commerce commerce;
@PostConstruct
void init() {
commerce = new Commerce("commerce.db");
}
@PreDestroy
void cleanup() {
if (commerce != null) {
commerce.close();
}
}
@Produces
@ApplicationScoped
public Commerce commerce() {
return commerce;
}
}
Enterprise E-commerce Integration
SAP Hybris / SAP Commerce Cloud
public class StateSetProductFacade implements ProductFacade {
private final Commerce commerce;
@Override
public ProductData getProductForCode(String code) {
return commerce.products().get(code)
.map(this::toProductData)
.orElseThrow(() -> new UnknownIdentifierException(code));
}
private ProductData toProductData(Product product) {
ProductData data = new ProductData();
data.setCode(product.getSku());
data.setName(product.getName());
data.setPrice(createPrice(product.getBasePrice()));
return data;
}
}
Broadleaf Commerce
@Component
public class StateSetInventoryService implements InventoryService {
private final Commerce commerce;
@Override
public boolean isAvailable(Sku sku, int quantity) {
return commerce.inventory().get(sku.getId())
.map(item -> item.getQuantityAvailable() >= quantity)
.orElse(false);
}
@Override
public void decrementInventory(Sku sku, int quantity) {
commerce.inventory().adjust(
sku.getId(),
-quantity,
"Order fulfillment"
);
}
}
Platform Support
| Platform |
Architecture |
Status |
| Linux |
x86_64 |
✅ |
| Linux |
arm64 |
✅ |
| macOS |
x86_64 |
✅ |
| macOS |
arm64 (M1+) |
✅ |
| Windows |
x86_64 |
✅ |
Building from Source
Prerequisites
- Rust 1.70+
- JDK 11+
- Gradle 8.0+
Build
cd bindings/java
cargo build --release
cd java
./gradlew build
Run Tests
./gradlew test
Thread Safety
The Commerce class and all its APIs are thread-safe. The underlying Rust engine uses proper synchronization. You can safely share a single Commerce instance across multiple threads.
ExecutorService executor = Executors.newFixedThreadPool(10);
Commerce commerce = new Commerce("commerce.db");
for (int i = 0; i < 100; i++) {
final int idx = i;
executor.submit(() -> {
commerce.products().create(
"SKU-" + idx,
"Product " + idx,
9.99
);
});
}
Error Handling
All errors are thrown as StateSetException:
try {
commerce.inventory().adjust("NONEXISTENT", -100, "Test");
} catch (StateSetException e) {
System.err.println("Error: " + e.getMessage());
}
Performance
- Embedded SQLite: No network latency, sub-millisecond queries
- Native JNI: Direct memory access, minimal marshalling overhead
- Thread-safe: Concurrent access with proper locking
- Lazy loading: APIs are instantiated on first access
License
MIT License - see LICENSE file for details.
Links