A question on applying Residue Theorem to definite real integral I came across this question here and even though it was not my own question it intrigued me so much that I tried to solve it:

Using the Residue Theorem find 
$$\int_0^\infty \frac{x \sin(x)}{x^2+a^2} dx$$

But I got stuck and questions came up. Here are my thoughts:
First, I think the idea of applying the residue theorem would be to define a curve that is the boundary of one half of a disk centered at the origin. Then one computes the integral of $f$ around this half disk using the residue theorem. Finally, one uses this result by taking the limit of the radius towards infinity to find the integral in the question.

Do I understand the idea of how to approach this problem correctly?

Here is how I tried to implement this:
First, we note that the poles are at $\pm ia$. So we want to choose the radius of the disk bigger than $a$. For example, $2a$ should do. 
Then the curves that together form the contour (CCW) are:
$$ \gamma_1(t) = -2a + 2at, t \in [0,1]$$
and
$$ \gamma_2(t) = 2a e^{it}, t \in [0,\pi]$$
Then 
$$ \oint_C f(z) dz = 2 \pi i \text{Res}_f (ia) = 2 \pi i \lim_{z \to ia} (z - ia) \frac{z \sin(z)}{z^2+a^2} = 2 \pi i \lim_{z \to ia}  \frac{z \sin(z)}{(z+ia)} =  \pi i { \sin(ia) }$$
Then
$$\begin{align} 
\int_{-2a}^{2a}f(z) dz &=  \oint_C f(z) dz - \oint_{\gamma_2} f(z) dz  \\
&=  \pi i { \sin(ia) } - \int_0^\pi {\gamma_2(t) \sin (\gamma_2(t)) \over \gamma_2^2(t) + a^2} 2ai e^{it} dt\\
&=\pi i { \sin(ia) } - \int_0^\pi {2a e^{it} \sin (2a e^{it}) \over 4a^2 e^{2it} + a^2} 2ai e^{it} dt\\
&=\pi i { \sin(ia) } - \int_0^\pi {2a e^{it} \sin (2a e^{it}) \over (2a e^{it} + ia)(2ae^{it} - ia)} 2ai e^{it} dt
\end{align}$$
And this is where I got stuck. I can't calculate $ \int_0^\pi {2a e^{it} \sin (2a e^{it}) \over (2a e^{it} + ia)(2ae^{it} - ia)} 2ai e^{it} dt$. This integral seems impossible to solve.

Did I make a mistake in the approach? Or did I miscalculate somewhere?
  Or is there a trick to solve integrals like this one?

 A: There is a trick to circumvent the difficulty presented in the OP.  We note that $\sin(x)=\text{Im}(e^{ix})$.  Then, assuming that $a>0$, we evaluate the integral
$$\oint_{C_R} \frac{ze^{iz}}{z^2+a^2}\,dz=\int_{-R}^{R}\frac{xe^{ix}}{x^2+a^2}\,dx+\int_0^\pi \frac{Re^{it}e^{iRe^{it}}}{R^2e^{i2t}+a^2}\,iRe^{it}
\,dt \tag1$$
As $R\to \infty$, the second integral on the right-hand side of $(1)$ approaches $0$.  Therefore, find that
$$\begin{align}
2\pi i \text{Res}\left(\frac{ze^{iz}}{z^2+a^2},z=ia\right)&=2\pi i \frac{ia e^{-a}}{2ia}\\\\
&=i\pi e^{-a}\\\\
&=\int_{-\infty}^{\infty}\frac{xe^{ix}}{x^2+a^2}\,dx \tag 2
\end{align}$$
Taking the imaginary part of $(2)$ and exploiting the evenness of the integrand reveals
$$\int_{0}^{\infty}\frac{x\sin(x)}{x^2+a^2}\,dx=\frac{\pi e^{-a}}{2}$$
A: It has been too long since I have taken complex analysis.  But, it looks like you have evaluated the residual correctly.  The next step.
your function is an even function.
$\int_0^{\infty} f(z)dz = \frac12\int_{-\infty}^{\infty} f(z)dz$
If we make the contour the half circle above the x axis with radius R, + the real line from -R to R.
The semi-circle can be parameterized with z = Re^it
$\oint f(z) dz = \int_{-R}^{R} f(z)dz + \int_{0}^{\pi} f(Re^{it}) (iRe^{it}) dt$
$\pi sinh a =  \int_{-R}^{R} f(z)dz + \int_{0}^{\pi} f(Re^{it}) (iRe^{it}) dt$
What you hope is to be able to show that $\int_{0}^{\pi} f(Re^{it}) (iRe^{it}) dt$ goes to $0$ as R gets to be very large.
Now, if you didn't have a z in the numerator it would be simple.
but with what you have
$\int_{0}^{\pi} \dfrac {(R e^{it}) \sin (Re^{it}) (iRe^{it})}{R^2 e^{2it} + a^2} dt$
$\int_{0}^{\pi} \dfrac {\sin (Re^{it}) }{1 + \frac {a^2}{R^2} e^{-2it}} dt$
as R goes to infinity $\frac {a^2}{R^2} e^{-2it}$ goes to $0$
leaving.
$\int_{0}^{\pi} \sin (Re^{it})dt$
And I don't know what to do with that.
A: I have a non-complex analysis solution using Feynmann Technique.
Let $\displaystyle I(a,b)=\int_{0}^{\infty}\frac{\sin(ax)}{x(x^{2}+b)}dx$
So we have :-
$\displaystyle \frac{\partial I}{\partial a} = \int_{0}^{\infty}\frac{\cos(ax)}{x^{2}+b} dx$
$\displaystyle \frac{\partial^{2}I}{\partial a^{2}} = -\int_{0}^{\infty}\frac{x\sin(ax)}{x^{2}+b}dx=
-\int_{0}^{\infty} \frac{\sin(ax)}{x}dx + b\int_{0}^{\infty}\frac{\sin(ax)}{x^{2}+b} dx
= -\frac{\pi}{2} + bI(a,b)$
Here I used the fact that $\displaystyle \int_{0}^{\infty}\frac{\sin(ax)}{x} = \frac{\pi}{2}$
So we have $\displaystyle (D^{2}-b)I = \frac{-\pi}{2}$  Here $\displaystyle D\equiv \frac{\partial}{\partial a}$
Solving this ODE in a we get
$\displaystyle I(a,b) =C_{1}e^{a\sqrt{b}} + C_{2}e^{-a\sqrt{b}} +\frac{\pi}{2b}$
We have $I(0,b) = 0$
So $\displaystyle C_{1}+C_{2} + \frac{\pi}{2b} = 0$
Also $\displaystyle \frac{\partial I}{\partial a} = \sqrt{b}(C_{1}e^{a\sqrt{b}} -C_{2}e^{-a\sqrt{b}})$
Also $\displaystyle \frac{\partial I}{\partial a}$ at $(0,b)$ = $\int_{0}^{\infty}\frac{dx}{x^{2}+b} = \frac{\pi}{2\sqrt{b}}$.
So we get $C_{1}=0$ and $C_{2} = \frac{-\pi}{2b}$
So $\displaystyle I(a,b) = \frac{-\pi}{2b}e^{-a\sqrt{b}} + \frac{\pi}{2b}$
$\displaystyle \frac{\partial I}{\partial a } = \frac{\pi}{2\sqrt{b}}e^{-a\sqrt{b}}$
$\displaystyle \frac{\partial^{2} I}{\partial a^{2}} = \frac{-\pi}{2}e^{-a\sqrt{b}}$
So as per the problem we need to replace $a=1$ in the above expression and multiply it with $-1$ to get our answer as :-$\displaystyle \frac{\pi}{2}e^{-\sqrt{b}}$. Also here $b=a^{2}$ . So our answer is $\displaystyle \frac{\pi}{2}e^{-a}$
