Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Events are fired a few times

Rafal S February 20, 2013

I'm writing my own listener.

When I debug my listener in atlassian SDK event issueUpdated is fired two times (I did only one change on one field). When I install plugin in my own environment Jira, this event it's fired 7 times. Why does it happen? I'd like to be fired only once.

My code of listener:

package com.axa.plugins;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.ofbiz.core.entity.GenericEntityException;
import org.ofbiz.core.entity.GenericValue;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.axa.jira.operations.JiraChange;
import com.axa.jira.operations.JiraComment;
import com.axa.jira.operations.JiraCustom;
import com.axa.pojo.ActionInfo;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.issue.IssueEventListener;


public class JiraListenerImpl extends AbstractIssueEventListener implements 
								IssueEventListener, InitializingBean, DisposableBean,
								JiraListener
{
	private final EventPublisher eventPublisher;
	private PropertiesConfiguration config = null;
	
	private static final String DELIMETER = ";";
	
	// config.properties
	private static final String KEY_CUSTOM_EVENT_TYPE_ID_ACTION = "customEventTypeId.action";
	
	
	/**
     * Constructor.
     * @param eventPublisher injected {@code EventPublisher} implementation.
     * @throws ConfigurationException 
     */
    public JiraListenerImpl(EventPublisher eventPublisher) throws ConfigurationException 
    {
        this.eventPublisher = eventPublisher;
        
        config = new PropertiesConfiguration();
    	try {
			config.load(JiraListenerImpl.class.getResourceAsStream("/jiralistener.properties"));
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	  	
    }
 
    /**
     * Called when the plugin has been enabled.
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception 
    {
        // register ourselves with the EventPublisher
        eventPublisher.register(this);
    }
 
    /**
     * Called when the plugin is being disabled or removed.
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception 
    {
        // unregister ourselves with the EventPublisher
        eventPublisher.unregister(this);
    }
    
   
    
    @Override
    public void issueCreated(IssueEvent issueevent)
    {
    	
    }

    @Override
    public void issueUpdated(IssueEvent issueevent)
    {
    	List<GenericValue> changeItemList = null;
    	GenericValue changeGroup = null;
    	JiraChange jiraChange = null;
    	ActionInfo actionInfo = null;
    	
    	changeGroup = issueevent.getChangeLog();
    	
        try
        {
          changeItemList = issueevent.getChangeLog().getRelated( "ChildChangeItem" );
        }
        catch( GenericEntityException e )
        {
          // Error or do what you need to do here.
          e.printStackTrace();
        }
        
        if( changeItemList == null )
        {
          // Same deal here.
          return;
        }

        Iterator<GenericValue> changeItemListIterator = changeItemList.iterator();
        while( changeItemListIterator.hasNext() )
        {
          GenericValue changeItem = ( GenericValue )changeItemListIterator.next();
          String fieldName = changeItem.get( "field" ).toString();
          Object oldValue = changeItem.get( "oldvalue" );
          Object newValue = changeItem.get( "newvalue" );
          Object oldString = changeItem.get( "oldstring" );
          Object newString = changeItem.get( "newstring" );
          Object who = changeGroup.get("author");
          String when = changeGroup.get("created").toString();
          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
  	      Date convertedDate = new Date();
  	      try 
  	      {
  	    	  	convertedDate = dateFormat.parse(when);
  	      } catch (ParseException e) 
  	      {
  	        // TODO Auto-generated catch block
  	        e.printStackTrace();
  	      }
          String jiraKey = issueevent.getIssue().getKey();
 		  String jiraURLSource = ComponentManager.getInstance().
 				  			getApplicationProperties().getString("jira.baseurl");
 		  actionInfo = new ActionInfo(jiraURLSource, jiraKey);
 	          
          jiraChange = new JiraChange(fieldName, (String) oldValue, (String) newValue, 
        		  (String) oldString, (String) newString, (String) who, convertedDate, 
        		  actionInfo);
          
          jiraChange.post();
          
        }
    }

    @Override
    public void issueAssigned(IssueEvent issueevent)
    {
    }

    @Override
    public void issueResolved(IssueEvent issueevent)
    {
    	
    }

    @Override
    public void issueClosed(IssueEvent issueevent)
    {
    	
    }

    @Override
    public void issueCommented(IssueEvent issueevent)
    {
    	
    }

    @Override
    public void issueCommentEdited(IssueEvent issueevent)
    {
    }

    @Override
    public void issueWorklogUpdated(IssueEvent issueevent)
    {
    }

    @Override
    public void issueWorklogDeleted(IssueEvent issueevent)
    {
    }

    @Override
    public void issueReopened(IssueEvent issueevent)
    {
    	
    }

    @Override
    public void issueDeleted(IssueEvent issueevent)
    {
    }

    @Override
    public void issueWorkLogged(IssueEvent issueevent)
    {
    }

    @Override
    public void issueStarted(IssueEvent issueevent)
    {
    }

    @Override
    public void issueStopped(IssueEvent issueevent)
    {
    }

    @Override
    public void issueMoved(IssueEvent issueevent)
    {
    }

    @Override
    public void issueGenericEvent(IssueEvent issueevent)
    {
    }
    
    @Override
    public void customEvent(IssueEvent issueevent)
    {
    }
    
}

6 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Rafal S February 27, 2013

Any ideas to resolve the problem?

0 votes
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 24, 2013

Did you check the workflow if you have any other customizations throwing issue updated events?

Rafal S February 24, 2013

Yes, I did. I use default workflow (jira) and it hasn't any customizations throwing issue updated events

Rafal S February 26, 2013

Any ideas to resolve the problem?

0 votes
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2013

Well, I don't know what jiraChange.post() does but check that an event is not thrown again. You might be getting a loop.

0 votes
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2013

Well, I don't know what jiraChange.post() does but check that an event is not thrown again. You might be getting a loop.

Rafal S February 24, 2013

jiraChange.post() send JSON of object to mule. I'm sure there isn't loop, which calls event again. Here is code:

public void post()
	{
		Configuration configuration = new Configuration();
		HttpClient httpClient = new HttpClient(configuration.getTargetUrl(),
				this.toJSON());
		httpClient.sendPostRequest();
	}

public void sendPostRequest()
	{
        try {
            
            // Send the request
        	URL url = new URL(this.url);
        	URLConnection conn = url.openConnection();
            conn.setDoOutput(true);

            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

            writer.write(this.content);
            writer.flush();
            
            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
             //System.out.println(line);
            }
            writer.close();
            reader.close();
            
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
	}

0 votes
Rafal S February 21, 2013

I included my code to the descirption

0 votes
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2013

Without the code, it's hard to say.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events