What is java callback method

Callback method example in Java

Here I am showing a simple example on what is callback method in Java. A callback is a piece of code that you can pass as an argument to be executed on some other code. Since Java doesn’t yet support function pointer, the callback methods are implemented as command objects.

A callback will usually hold reference to some state to which it actually becomes useful.

By making the callback implementation in your code, you gain indirection between your code and the code that is executing the callback.

So, a callback method can be any method in any class; a reference to an instance of the class is maintained in some other class, and, when an event happens in other class, it calls the method from the first class.

The only thing that makes it a callback as distinguished from any other method call is the idea of handling a reference to an object for the purpose of having that object invokes a method on it later, usually on some events.

Let’s say you are calling a method from another method, but the key is that the callback is treated like an Object, so you can change which method to call based on the state of the system (like the Strategy Design Pattern).

Читайте также:  Open html file online

Callback Method Example

My own Callable interface.

package com.roytuts.java.callback.method; interface Callable

Worker class who calls the callback.

package com.roytuts.java.callback.method; public class Worker < public void doWork(Callable callable) < System.out.println("Doing Work"); callable.call(); >>
package com.roytuts.java.callback.method; public class CallBackMethodApp < public static void main(String[] args) < new Worker().doWork(new Callable() < @Override public void call() < System.out.println("Callback Called"); >>); > >

Executing the above main class will give you the following output:

Doing Work Callback Called

Hope you got an idea how to write callback method in Java programming language.

Источник

Урок 13. Основы JAVA. Методы обратного вызова (callback)

Механизм обратного вызова(callbacks) широко распространен в программировании. При обратном вызове программист задает действия, которые должны выполняться всякий раз, когда происходит некоторое событие. И неважно, будете разрабатывать только java-программы или android-приложения – колбеки будут встречаться вам повсюду.

Для создания методов обратного вызова (callback) в java используются уже знакомые нам из прошлого урока интерфейсы.

import javax.swing.*; public class SomeClass < // создаем колбек и его метод interface Callback< void callingBack(); >Callback callback; public void registerCallBack(Callback callback) < this.callback = callback; >void doSomething() < JOptionPane.showMessageDialog(null, "Выполняется работа"); // вызываем метод обратного вызова callback.callingBack(); >>

У интерфейса мы определили один метод callingBack(). Далее в классе мы создали объект интерфейса и инициализировали его в методе registerCallBack. В классе также был создан метод doSomething(), в котором может содержаться какой-то сложный код. Мы здесь создаем диалоговое окно, используя компонент JOptionPane библиотеки Swing. Кто не в курсе, Swing — библиотека для создания графического интерфейса программ на языке Java.

В конце работы метода doSomething() вызывается метод интерфейса callingBack() . В данном случае мы сами создали метод и знаем его код. Но во многих случаях, вы будете использовать готовый метод какого-то класса и можете даже не знать, что именно содержится в этом методе. Вам надо только знать, что такой метод существует и выполняет конкретную задачу.

Теперь нам нужен класс, реализующий интерфейс-callback. Создаем класс MyClass и подключаем интерфейс-колбек через ключевое слово implements. Среда разработки заставит нас реализовать шаблон метода интерфейса.

public class MyClass implements SomeClass.Callback < @Override public void callingBack() < System.out.println("Вызов метода обратного вызова"); >>

Теперь мы можем использовать метод обратного вызова callingBack () для решения своих задач. Будем здесь выводить текст в консоль. Создадим класс Main, в котором будет реализован метод main – точка входа в программу.

Здесь создаем экземпляры классов, инициализируем колбек, передавая методу registerCallBack экземпляр MyClass, реализующий интерфейс-callback. И вызываем метод doSomething();

При старте программы будет выполняться код метода doSomething(); из класса SomeClass. На экране появилось диалоговое окно.

2016-06-28_19-17-36

Когда мы нажмем кнопку, или закроем диалоговое окно, сработает метод обратного вызова callingBack(), который выведет сообщение в консоль.

package callback; import javax.swing.*; public class SomeClass < String replyTo; interface Callback< void callingBack(String s); >private Callback callback; public void registerCallBack(Callback callback) < this.callback = callback; >void doSomething() < int reply = JOptionPane.showConfirmDialog(null, "Вы программист?", "Опрос", JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.NO_OPTION)< replyTo = "Нет"; >if (reply == JOptionPane.YES_OPTION) < replyTo = "Да"; >callback.callingBack(replyTo); > >

Изменим диалоговое окно – присвоим его новой целочисленной переменной reply, и изменим на showConfirmDialog, который имеет заголовок окна и больше одной кнопки ответа. В зависимости от нажатой кнопки переменной reply будет присвоено значение. Сравниваем ее значение с константой и присваиваем значение “Да” или “Нет” переменной replyTo.

package callback; public class MyClass implements SomeClass.Callback < @Override public void callingBack(String s) < if (s != null) < System.out.println("Ваш ответ: " + s); >else < System.out.println("Вы не ответили на вопрос!"); >> >

Добавим аргумент в метод. И добавим условие проверки, что строка не пустая – это возможно, если вместо ответа просто закрыть диалоговое окно.

package callback; public class Main < public static void main(String[] args) < SomeClass someClass = new SomeClass(); MyClass myClass = new MyClass(); someClass.registerCallBack(myClass); someClass.doSomething(); >>

Запускаем программу. Теперь текст сообщения в консоли меняется в зависимости от того, выберем мы да, нет или просто закроем окно.

2016-06-28_19-13-28

Я надеюсь, вы поняли принцип методов обратного вызова. Ниже есть ссылка на дополнительную информацию по колбекам, а также более сложные и познавательные примеры. Вопросы можно задавать в комментариях.

Источник

How to use callbacks in Java

Red rotary phone, ringing, calling, callback

A callback operation in Java is one function that is passed to another function and executed after some action is completed. A callback can be executed either synchronously or asynchronously. In the case of a synchronous callback, one function is executed right after another. In the case of an asynchronous callback, a function is executed after an undetermined period of time and happens in no particular sequence with other functions.

This article introduces you to callbacks in Java, starting with the classic example of the callback as a listener in the Observable design pattern. You will see examples of a variety of synchronous and asynchronous callback implementations, including a functional callback using CompletableFuture .

Synchronous callbacks in Java

A synchronous callback function will be always executed right after some action is performed. That means that it will be synchronized with the function performing the action.

As I mentioned, an example of a callback function is found in the Observable design pattern. In a UI that requires a button click to initiate some action, we can pass the callback function as a listener on that button click. The listener function waits until the button is clicked, then executes the listener callback.

Now let’s look at a few examples of the callback concept in code.

Anonymous inner class callback

Anytime we pass an interface with a method implementation to another method in Java, we are using the concept of a callback function. In the following code, we will pass the Consumer functional interface and an anonymous inner class (implementation without a name) to implement the accept() method.

Once the accept() method is implemented, we’ll execute the action from the performAction method; then we’ll execute the accept() method from the Consumer interface:

 import java.util.function.Consumer; public class AnonymousClassCallback < public static void main(String[] args) < performAction(new Consumer() < @Override public void accept(String s) < System.out.println(s); >>); > public static void performAction(Consumer consumer) < System.out.println("Action is being performed. "); consumer.accept("Callback is executed"); >> 

The output from this code is the print statement:

 Action is being performed. Callback is executed. 

In this code, we passed the Consumer interface to the performAction() method, then invoked the accept() method after the action was finished.

You might also notice that using an anonymous inner class is quite verbose. It would be much better to use a lambda instead. Let’s see what happens when we use the lambda for our callback function.

Lambda callback

In Java, we can implement the functional interface with a lambda expression and pass it to a method, then execute the function after an operation is finished. Here’s how that looks in code:

 public class LambdaCallback < public static void main(String[] args) < performAction(() ->System.out.println("Callback function executed. ")); > public static void performAction(Runnable runnable) < System.out.println("Action is being performed. "); runnable.run(); >> 

Once again, the output states that the action is being performed and the callback executed.

In this example, you might notice that we passed the Runnable functional interface in the performAction method. Therefore, we were able to override and execute the run() method after the action from the performAction method was finished.

Asynchronous callbacks

Often, we want to use an asynchronous callback method, which means a method that will be invoked after the action but asynchronously with other processes. That might help in performance when the callback method does not need to be invoked immediately following the other process.

Simple thread callback

Let’s start with the simplest way we can make this asynchronous callback call operation. In the following code, first we will implement the run() method from a Runnable functional interface. Then, we will create a Thread and use the run() method we’ve just implemented within the Thread . Finally, we will start the Thread to execute asynchronously:

 public class AsynchronousCallback < public static void main(String[] args) < Runnable runnable = () ->System.out.println("Callback executed. "); AsynchronousCallback asynchronousCallback = new AsynchronousCallback(); asynchronousCallback.performAsynchronousAction(runnable); > public void performAsynchronousAction(Runnable runnable) < new Thread(() ->< System.out.println("Processing Asynchronous Task. "); runnable.run(); >).start(); > > 

The output in this case is:

 Processing Asynchronous Task. Callback executed. 

Notice in the code above that first we created an implementation for the run() method from Runnable . Then, we invoked the performAsynchronousAction() method, passing the runnable functional interface with the run() method implementation.

Within the performAsynchronousAction() we pass the runnable interface and implement the other Runnable interface inside the Thread with a lambda. Then we print «Processing Asynchronous Task…» Finally, we invoke the callback function run that we passed by parameter, printing «Callback executed…»

Asynchronous parallel callback

Other than invoking the callback function within the asynchronous operation, we could also invoke a callback function in parallel with another function. This means that we could start two threads and invoke those methods in parallel.

The code will be similar to the previous example but notice that instead of invoking the callback function directly we will start a new thread and invoke the callback function within this new thread:

 // Omitted code from above… public void performAsynchronousAction(Runnable runnable) < new Thread(() ->< System.out.println("Processing Asynchronous Task. "); new Thread(runnable).start(); >).start(); > 

The output from this operation is as follows:

 Processing Asynchronous Task. Callback executed. 

The asynchronous parallel callback is useful when we don’t need the callback function to be executed immediately after the action from the performAsynchronousAction() method.

A real-world example would be when we purchase a product online and we don’t need to wait until the payment is confirmed, the stock being checked, and all those heavy loading processes. In that case, we can do other things while the callback invocation is executed in the background.

CompletableFuture callback

Another way to use an asynchronous callback function is to use the CompletableFuture API. This powerful API, introduced in Java 8, facilitates executing and combining asynchronous method invocations. It does everything we did in the previous example such as creating a new Thread then starting and managing it.

In the following code example we will create a new CompletableFuture , then we’ll invoke the supplyAsync method passing a String .

Next, we will create another , CompletableFuture that will thenApply a callback function to execute with the first function we configured:

 import java.util.concurrent.CompletableFuture; public class CompletableFutureCallback < public static void main(String[] args) throws Exception < CompletableFuturecompletableFuture = CompletableFuture.supplyAsync(() -> "Supply Async. "); CompletableFuture execution = completableFuture .thenApply(s -> s + " Callback executed. "); System.out.println(execution.get()); > > 
 Supply Async. Callback executed… 

Conclusion

Callbacks are everywhere in software development, vastly used in tools, design patterns, and in applications. Sometimes we use them without even noticing it.

We’ve gone through a variety of common callback implementations to help demonstrate their utility and versatility in Java code. Here are some features of callbacks to remember:

  • A callback function is supposed to be executed either when another action is executed or in parallel to that action.
  • A callback function can be synchronous, meaning that it must be executed right after the other action without any delay.
  • A callback function can be asynchronous, meaning that it can be executed in the background and may take some time until it’s executed.
  • The Observable design pattern uses a callback to notify interested entities when an action has happened.

Next read this:

Copyright © 2023 IDG Communications, Inc.

Источник

Оцените статью