Asynchronous Apex 04

🚀 Control Processes with Queueable Apex

프로세스 제어 with 대기 가능한 Apex

📚 Learning Objectives

  • When to use the Queueable interface
    언제 사용하는가 대기가능한(?) 인터페이스를
  • The differences between queueable and future methods
    차이점 - 대기가능함과 future 메서드의
  • Queueable Apex syntax
    Queueable Apex의 문법
  • Queueable method best practices
    Queueable 메서드 최고의 연습

📄 Queueable Apex


🎯 Challenge

AddPrimaryContact

public class AddPrimaryContact implements Queueable {
    
    private Contact con;
    private String state;
    
    //생성자 생성
    public AddPrimaryContact(Contact con, String state) {
        this.con = con;
        this.state = state;
    }
    
    //Queueable 실행장소
    public void execute(QueueableContext context) {
    	LIST<Account> accounts = [Select Id, Name, (Select FirstName, LastName, Id From contacts) 
                                  From Account Where BillingState = :state Limit 200];
        
        LIST<Contact> primaryContacts = new LIST<Contact>();
        for(Account acc : accounts){
            Contact c = con.clone();
            c.AccountId = acc.Id;
            primaryContacts.add(c);
        }
        
        if(primaryContacts.size() > 0){
            insert primaryContacts;
        }
        
        
    }
}

AddPrimaryContactTest

@isTest
public class AddPrimaryContactTest {
    
    static testmethod void testQueueable(){
        LIST<Account> testAccounts = new LIST<Account>();
        
        for(Integer i = 0; i < 50; i++){
            testAccounts.add(new Account(Name = 'Account ' + i, BillingState = 'CA'));
            
        }
        for(Integer j = 0; j < 50; j++){
            testAccounts.add(new Account(Name = 'Account ' + j, BillingState = 'CA'));
        }
        insert testAccounts;
        
        Contact testContact = new Contact(FirstName = 'John', LastName = 'Doe');
        insert testContact;
        
        AddPrimaryContact addit = new addPrimaryContact(testContact, 'CA');
        
        Test.startTest();
        System.enqueueJob(addit);
        Test.stopTest();
        
        System.assertEquals(50, [Select count() From Contact Where accountId in 
                                 (Select Id From Account Where BillingState = 'CA')]);
    }
    
    
}

🤔 Words

  • Queueable: 대기가능한(? 이게 무슨단어람???)

좋은 웹페이지 즐겨찾기