Spencer Mellors, 52, went to the Sou'wester Tavern pub in Glasgow on 11 September to get his dog from his ex-partner Anne Marie Dunn.
He pled guilty to behaving in a threatening or abusive manner and having a stun gun disguised as a torch.
In 1996, he was jailed for nine years for raping and biting a prostitute.
Glasgow Sheriff Court heard how Miss Dunn and her colleague were inside the pub around 00:20 when they heard someone at the shutters.
Mellors and Miss Dunn had been in a relationship for about 10 years until 2014.
Procurator fiscal depute Harry Findlay said Miss Dunn went outside and saw Mellors across the road beside a Glasgow City Council van.
She shouted to a council worker, Shaun Devine, that Mellors was next to his vehicle and returned to the pub.
Mr Findlay said: "Mellors then shouted and swore towards the door of the pub."
Mellors also shouted abuse at Mr Devine and his colleague.
The council workers had said they could see something in his hand they thought was a torch but were concerned, and phoned the police.
Mellors walked off and was found by police later that day, and later arrested.
The following day, after getting a search warrant, police found the stun gun at Mellors' home in Glasgow's east end.
Sheriff Bill Totten told him: "I am required to impose a sentence of five years in prison."
He also handed Mellors a non-harrassment order meaning he cannot contact or attempt to contact Miss Dunn.
A convicted rapist has been jailed for five years for being in possession of a stun gun while shouting abuse at his former girlfriend outside her work.