Executing or scheduling background task is always an important activity for developers.
There are so many scenarios where background tasks are used like import/export, sending bulk notification or email, processing orders etc.
You can find this interesting link here which talks in length for background tasks.
What it does not offer is implementation example and changes for background task in Liferay 7/DXP
By following few Liferay conventions, we can easily use this weapon.
In this example I will explain creation of background task, pass parameters and handle errors if any.
In next example I will demonstrate check status, progress and display it over UI.
We will be creating two osgi components for this activity.
1. Create osgi module for background task creation and register it to handler
2. Use above module to create background tasks
Render method to create background task every time portlet loads.
@Overridepublic void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { Random random = new Random(12); HttpServletRequest request = PortalUtil .getHttpServletRequest(renderRequest); ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest .getAttribute(WebKeys.THEME_DISPLAY); ServiceContext serviceContext = null; try { serviceContext = ServiceContextFactory.getInstance(renderRequest); } catch (PortalException e) { logger.error("Eror in getting service context", e.getCause()); } // This taskContextMap can be used as transporter to background job MaptaskContextMap = new HashMap<>(); taskContextMap.put("processName", "testing " + random.nextInt()); taskContextMap.put("totalNodes", String.valueOf(random.nextInt())); //taskContextMap.put("serviceContext", serviceContext); try { // Adding the job to liferay background manager com.liferay.portal.kernel.backgroundtask.BackgroundTask backgroundTask = backgroundTaskmanager.addBackgroundTask(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), SampleBackgroundTaskExecutor.class.getName(),SampleBackgroundTaskExecutor.class.getName(),taskContextMap, serviceContext); // With returned background object you can check status, id etc. renderRequest.setAttribute("backgroundTaskId", backgroundTask.getBackgroundTaskId()); } catch (PortalException e) { logger.error(e.getCause()); } catch (SystemException e) { logger.error(e.getCause()); }
Create Background Task Executor
@Component( immediate = true, property = {"background.task.executor.class.name=com.netcracker.cabinet.background.executor.MigrationBackgroundTaskExecutor"},// Without this property osgi will not register this as background executor/handler service = BackgroundTaskExecutor.class) public class SampleBackgroundTaskExecutor extends BaseBackgroundTaskExecutor {
isSerial - True
// if it's not serial then multiple instances of this executor can run parallel, to run it in queue mode, we use isSerial true@Overridepublic boolean isSerial() { return true; }
Main execute method for the Job
public BackgroundTaskResult execute(BackgroundTask backgroundTask) throws Exception { // taskContextMap which is sent by the caller MaptaskContextMap = backgroundTask.getTaskContextMap(); String taskName = (String)taskContextMap.get("processName") ; String totalNodes = (String)taskContextMap.get("totalNodes"); //ServiceContext serviceContext = (ServiceContext) taskContextMap.get("serviceContext"); if(LOGGER.isDebugEnabled()){ LOGGER.debug("Task Name : "+ taskName); } BackgroundTaskVO messageContent = new BackgroundTaskVO(); messageContent.setTotalNodes(totalNodes); // Sending the data to util for MessageBus SampleDataHandlerStatusMessageSenderUtil.sendStatusMessage(messageContent); // Telling the system if, background task is successful or not BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult( BackgroundTaskConstants.STATUS_SUCCESSFUL); backgroundTaskResult.setStatusMessage("Wonder full"); return backgroundTaskResult; }
Message Bus update
public static void sendStatusMessage(BackgroundTaskVO messageContent) { // Leave if no background task if (!BackgroundTaskThreadLocal.hasBackgroundTask()) { return; } // Message Creation Message message = createMessage(messageContent); // Send message to message bus MessageBusUtil.sendMessage(DestinationNames.BACKGROUND_TASK_STATUS, message); }
You can download the source code from here
You are just done, Try & Enjoy the function.............:)
Hi Buddy Nice article !! Just a question where at which point you are adding the real task like importing a file or anything else in this code ?
ReplyDeleteThanks for your great and helpful presentation I like your good service. I always appreciate your post. That is very interesting I love reading and I am always searching for informative information like this.AngularJS Training in Chennai | Best AngularJS Training Institute in Chennai
ReplyDeleteWow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog. Share more like this. Thanks Again.
ReplyDeleteiot training in Chennai | Best iot Training Institute in Chennai
Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeletemachine learning course in Chennai
machine learning training center in Chennai
machine learning certification in chennai
Android training in chennai
PMP training in chennai
Hi, Thanks a lot for your explanation which is really nice. I have read all your posts here. It is amazing!!!
ReplyDeleteKeeps the users interest in the website, and keep on sharing more, To know more about our service:
Please free to call us @ +91 9884412301 / 9600112302
Openstack course training in Chennai | best Openstack course in Chennai | best Openstack certification training in Chennai | Openstack certification course in Chennai | openstack training in chennai omr | openstack training in chennai velachery | openstack training in Chennai | openstack course fees in Chennai | openstack certification training in Chennai | best openstack training in Chennai | openstack certification in Chennai
I want to tell you how cool I spend my free time on this site is just super genteelonline slot games A bunch of slots and slot machines. Many people can even meet someone and of course have fun
ReplyDeleteGreat Post,really it was very helpful for us.
ReplyDeleteThanks a lot for sharing!
I found this blog to be very useful!!
JAVA training in Bangalore
Great blog, learned many things about background verification from this blog, very informative. The background verification in Chennai is one of the best places to get service in Chennai.
ReplyDeleteList Of Background Verification Companies In Chennai
Top 10 Background Verification Companies In Chennai
Background Check Companies In Chennai
Background Verification Companies In India
List Of Background Verification Companies In India
Background Verification Companies In Bangalore
Background Verification Companies In Hyderabad
Background Verification Companies In Mumbai
Background Verification Companies In Kerala
Nice information thank you,if you want more information please visit our link machine learning online course
ReplyDeleteYet, then again, these progressions will significantly affect everybody all over the place. In both individual and expert lives. machine learning course in pune
ReplyDeleteHi,Very nice post.Thanks for the information provided.It is very interesting and very informative.I am always impressed with your post and helpful tips.Keep posting tips and relevant content as usual.
ReplyDeletebig data training in btm
Great Article
ReplyDeleteB.Tech Final Year Projects for CSE in Angular
Angular Training in Chennai
Project Centers in Chennai
JavaScript Training in Chennai
I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... https://www.markedcardsforsale.com/marked-cards.html
ReplyDeleteThis is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: https://www.cardslenses.com/marked-cards.shtml
ReplyDeleteThanks for sharing is so amazing and helpful to us.
ReplyDeleteBuy Hydrocodone online
Great Article
ReplyDeleteData Mining Projects
Python Training in Chennai
Project Centers in Chennai
Python Training in Chennai
I have been checking out a few of your stories and i can state pretty good stuff. I will definitely bookmark your blog Digital Marketing Sydney
ReplyDeleteI really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. Thanks for sharing the information keep updating, looking forward to more posts.
ReplyDeleteHigh Quality Product Images
It is a great website.. The Design looks very good.. Keep working like that!. metal dice
ReplyDeleteThanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. d&d dice sets
ReplyDeletePositive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. dr martens mono black
ReplyDeleteSeeking to take your'office'outside to those times, or looking to work while travelling? mindmap templates
ReplyDeleteThis is the best blog for this topic and I really like your post. Thank you...
ReplyDeleteExcel Training in Chennai
Advanced Excel Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Pega Training in Chennai
Linux Training in Chennai
Graphic Design Courses in Chennai
Placement Training in Chennai
Social Media Marketing Courses in Chennai
Power BI Training in Chennai
Excel Training in Velachery
Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. that link
ReplyDeleteLocation, area, area is as yet the key of purchasing Miami real estate. Purchasing Miami real estate in light of the fact that the cost is low in a declining territory is serious mix-up that ought to be evaded. fast essay writing
ReplyDeleteĐại lý vé máy bay Aivivu
ReplyDeletekinh nghiệm mua vé máy bay đi Mỹ giá rẻ
vé máy bay tết
đường bay từ Việt Nam sang Pháp
vietnam airline đi hàn quốc
giá vé máy bay đi nhật bản vietnam airline
giá vé máy bay đi Anh
vé máy bay giá rẻ quốc tế
vé máy bay từ tpHCM đi San Francisco
giá vé máy bay đi Los Angesles
combo vinpearl nha trang 3 ngày 2 đêm 2020
Mua vé giá rẻ tại Aivivu, tham khảo
ReplyDeletekinh nghiệm mua vé máy bay đi Mỹ giá rẻ
mua vé về việt nam
lịch bay từ anh về việt nam hôm nay
chuyến bay từ Pairs về TpHCM
Really nice and informative blog, keep it up. Thanks for sharing and I have some suggestions.
ReplyDeleteif you want to learn Mobile App Development(android, iOS), Join Now Mobile App Training in Bangalore.
Visit a Website:- Android Training in Bangalore | AchieversIT
This may help you to find something useful
It is an excellent blog, I have ever seen. I found all the material on this blog utmost unique and well written. And, I have decided to visit it again and again. The Best Task Management Tool
ReplyDeleteI'm glad to see the great detail here!. The Best Remote Team Management Tool
ReplyDeleteKaliteli escort bayanlarla tanışma fırsatı yakalamak için tıkla: ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort
ReplyDeleteinstagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - minecraft premium hesap - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeleteI would like to say that this blog really convinced me to do it! Thanks, very good post. pg777th.com
ReplyDeleteGo play daftar slot online
ReplyDeleteinstagram takipçi satın al - instagram takipçi satın al - takipçi satın al - takipçi satın al - instagram takipçi satın al - takipçi satın al - instagram takipçi satın al - aşk kitapları - tiktok takipçi satın al - instagram beğeni satın al - youtube abone satın al - twitter takipçi satın al - tiktok beğeni satın al - tiktok izlenme satın al - twitter takipçi satın al - tiktok takipçi satın al - youtube abone satın al - tiktok beğeni satın al - instagram beğeni satın al - trend topic satın al - trend topic satın al - youtube abone satın al - beğeni satın al - tiktok izlenme satın al - sms onay - youtube izlenme satın al - tiktok beğeni satın al - sms onay - sms onay - perde modelleri - instagram takipçi satın al - takipçi satın al - tiktok jeton hilesi - pubg uc satın al - sultanbet - marsbahis - betboo - betboo - betboo
ReplyDeleteinstagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
takipçi satın al
takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
aşk kitapları
tiktok takipçi satın al
instagram beğeni satın al
youtube abone satın al
twitter takipçi satın al
tiktok beğeni satın al
tiktok izlenme satın al
twitter takipçi satın al
tiktok takipçi satın al
youtube abone satın al
tiktok beğeni satın al
instagram beğeni satın al
trend topic satın al
trend topic satın al
youtube abone satın al
beğeni satın al
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok beğeni satın al
sms onay
sms onay
perde modelleri
instagram takipçi satın al
takipçi satın al
tiktok jeton hilesi
pubg uc satın al
sultanbet
marsbahis
betboo
betboo
betboo
takipçi satın al
ReplyDeletetakipçi satın al
takipçi satın al
www.escortsmate.com
ReplyDeleteescortsmate.com
https://www.escortsmate.com
Ucuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla: https://www.ravje.com
ReplyDeleteİnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al
Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al
cover coin hangi borsada
ReplyDeletecover coin hangi borsada
cover coin hangi borsada
xec coin hangi borsada
xec coin hangi borsada
xec coin hangi borsada
ray hangi borsada
tiktok jeton hilesi
tiktok jeton hilesi
tiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
binance referans kimliği
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo
Takipçi satın al! Sende aşağıdaki bağlantıları kullanarak en güvenli takipçi satın alma sitesi Takipcidukkani.com ile takipçi satın al. Tıkla hemen sende instagram takipçi satın al:
ReplyDelete1- takipçi satın al
2- takipçi satın al
3 - takipçi satın al
2. defa geldim ve çok güzel yorumladım.
ReplyDeleteThanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends.
ReplyDeletetakipçi satın al
ReplyDeletepdf kitap indir
büyü kitapları pdf indir
kişisel gelişim kitapları pdf indir
https://kibriscanli.com