Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codenameone.developerguide.backend.messaging;

import com.codename1.backend.DataSource;
import com.codename1.backend.annotations.Component;
import com.codename1.backend.annotations.Transactional;
import com.codename1.backend.messaging.MessageTemplate;
import com.codenameone.developerguide.backend.orders.Order;

import java.io.IOException;

// tag::backend-messaging-send[]
@Component
public class Checkout {
private final DataSource db;
private final MessageTemplate messages;

public Checkout(DataSource db, MessageTemplate messages) {
this.db = db;
this.messages = messages;
}

@Transactional
public void place(Order order) throws IOException {
db.execute("INSERT INTO orders (id, customer) VALUES (?, ?)",
new Object[] {Long.valueOf(order.id), order.customer});
// Written to the outbox in this transaction, sent once it commits.
messages.send("orders.placed", order.customer, order);
}
}
// end::backend-messaging-send[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codenameone.developerguide.backend.messaging;

import com.codename1.backend.annotations.CacheEvict;
import com.codename1.backend.annotations.Cacheable;
import com.codename1.backend.annotations.Component;
import com.codenameone.developerguide.backend.orders.Order;

// tag::backend-cache[]
@Component
public class OrderLookup {
@Cacheable(value = "orders", key = "#p0")
public Order find(long id) {
return load(id);
}

@CacheEvict(value = "orders", key = "#p0.id")
public void update(Order order) {
// write the change; the next find() reads it again
}
// end::backend-cache[]

private Order load(long id) {
Order o = new Order();
o.id = id;
return o;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codenameone.developerguide.backend.messaging;

import com.codename1.backend.annotations.Component;
import com.codename1.backend.annotations.KafkaListener;
import com.codename1.backend.annotations.MessageListener;
import com.codename1.backend.messaging.Message;
import com.codenameone.developerguide.backend.orders.Order;

// tag::backend-messaging-listen[]
@Component
public class Shipping {
@MessageListener(value = "orders.placed", group = "shipping", concurrency = 4)
public void placed(Order order) {
// runs once per message; throwing has it delivered again later
}

@MessageListener(value = "orders.placed.dlq", group = "support")
public void gaveUp(Message message) {
String why = message.getHeader(Message.ERROR);
// the order that failed every attempt, and the last exception
}
}
// end::backend-messaging-listen[]

// tag::backend-messaging-spring[]
@Component
class Invoicing {
@KafkaListener(topics = "orders.placed", groupId = "invoicing")
public void placed(Order order) {
// the same listener, written as a Spring Kafka application writes it
}
}
// end::backend-messaging-spring[]
8 changes: 8 additions & 0 deletions docs/demos/backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
cn1.datasource.url=${DATABASE_URL}
cn1.server.port=8080
# end::backend-config-base[]

# tag::backend-scale-out[]
# One Redis for the cache, sessions, rate limits and scheduler locks...
cn1.kv.url=${REDIS_URL}
cn1.session.store=kv
# ...and a broker for messages.
cn1.messaging.url=kafka://kafka-1:9092,kafka-2:9092
# end::backend-scale-out[]
Loading
Loading